share argument between all fragments - android

I am making a bottom navigation app and want to share data between all fragments, for example clicking a button to share a name for all action bars. With safeArgs I can only send data to one fragment.
And I want to ask, is any solutions for global arguments(global variables) like redux in react native or provider in flutter?
Thank you

You can use shareviewmodel, it's best practice
Link refer: viewmodel

EDITED
Just create a public variable as a class variable in your MainActivity
public class MainActivity extends AppCompatActivity{
public int myVar; // outside of all methods
...
Then you have access it everywhere in your Fragments inside that MainActivity, like this:
MainActivity mainActivity = ((MainActivity) getActivity());
if (mainActivity != null)
mainActivity.myVar = 15;

Related

Reuse single ViewModel with multiple fragments. MvvmCross Android

I'm newbie with MvvmCross. I'm developing an app using Mvx v6.0.1.
I want to make a kind of stepper using fragments (specifically 4 fragments or 4 steps), all of these fragments are embedded into an main activity. The main activity layout has a button, when I tap these button fragment is switched to the next step. I have created one viewmodel for each fragment and one more for the main activity. The problem I have is that I need to get some values from each step and pass to the next step.
I'm trying to share only one viewmodel for every view (fragments and activity). Is there any way to achieve this?
If yes, How can I distinguish each steps/fragments for navigate across them?
I was trying to set same viewmodel to Activity and fragment:
//Activity code
public class AttendanceActivity : BaseActivity<AttendanceViewModel>
{
protected override int ActivityLayoutId => Resource.Layout.activity_attendance;
//some logic
}
//Fragment code
[MvxFragmentPresentation(ActivityHostViewModelType = typeof(AttendanceViewModel),
FragmentContentId = Resource.Id.attendance_content_frame,
AddToBackStack = true)]
public class AttendanceFragmentSetpOne : BaseFragment<AttendanceViewModel>, IOnClickListener
{
protected override int FragmentLayoutId => Resource.Layout.fragment_attendance_step_one;
//Some logic
}
But when I make this, the app never pass from splash screen.
I hope explain myself and somebody help me with this "problem".
Thanks in advance.

Android, should all code be written in an Activity

I am more familiar with iOS development than Android and I am wondering if all code should be written in an Activity rather than having a "model" class.
I have a couple screens each with a few checkboxes and I want them all to behave the same on click, I am trying to figure out how I would do this without writing repeating code in each activity. Thanks!
No you should not. If you are familiar with java, think of an activity as a extension of main with OO added.
In your particular example you can create a class with a method like:
<MethodName>(View <checkboxClickedName>){ //your code here }. and then add this to the checkbox in the XML android:onClick="<MethodName>", you may need the full package path (e.g. com.example.app.)
Note: if some of the commands/objects you need are only available within an activity you should create this in an calss that extends Activity or preferably within the running activity.
You could have a base class that extends activity that contains the methods that you want executed on click (either implemented or abstract). Use this new base class instead of activity when making new activities. In the layout xml, you can set the onclick of each checkbox to be the method in the base activity you want executed.
The best practice would be to use a single activity and switch fragments as if they were your screens. Then, the activity could simply implement the listener interface that the fragments would re-use.
Since you have multiple activities this becomes a little bit harder. To really re-use a single listener, I can think of a single (not so beautiful) option. Create a static listener and lazy load it:
public class MainActivity extends Activity {
private static View.OnClickListener sCheckboxClickListener;
public static View.OnClickListener getCheckboxClickListener() {
if (sCheckboxClickListener == null) {
sCheckboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Work with v
}
};
}
return sCheckboxClickListener;
}
}
And in each of your activities call:
findViewById(R.id.checkbox1)
.setOnClickListener(MainActivity.getCheckboxClickListener());

Functions for an activity in a separate class

I have a MainMenuActivity with multiple fragments. I want to put my code for the fragment "Home" in a separate class. This is my current approach.
public class Home extends MainMenuActivity{
public TextWatcher tw = new TextWatcher() {...};
public Home(){
}
public void buttonPressed(View view){...}
}
I want to add a TextWatcher to a EditText and functions which are executed, when a button is pressed
(defined in the xml: android:onClick="buttonPressed").
If this approach is possible, where and how can I create this class and should I put something in the Home constructor?
You should use Fragments in your project. Here are some examples :
http://developer.android.com/training/basics/fragments/creating.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
For your example you need to change MainMenuActivity to MainMenuFragmentActivity and create Home Fragment.
Best wishes.
With Java you shouldn't be afraid to have very large files (check View.java from the android source code, it's more than 15,000 lines long). There's no way to split a class like there is in C#.
But you say you use fragments. Why is your activity code so large? Usually most of the code go in the fragments, not in the activity.

Android Tabs and global variables

I am using android TabWidget and I have one main activity where I am managing all the tabs.
Each tab has his own activity:
private void setTabs()
{
addTab("News", NewsActivity.class);
addTab("Project", ProjectActivity.class);
addTab("About", AboutActivity.class);
addTab("Contact", ContactActivity.class);
}
The data for each tab is downloaded from the server in JSON format. My goal is to download all the data on the oppening of the application and then to use it for each tab (activity).
Is there a possibility to share those variables from the main activity to the others where I am managing the tabs? I know about using the Intent class but may be there is another easier solution with the tabs.
Thank you very much.
Declare static variables in your main activity and store the data in them.
For example, public static integer myInt;
myInt = ...;
Now when you go to the other Activities, just use MainActivity.myInt to access its value and even update it if you want. No need to pass the variable around using intents.
You can create a class that would hold any variables or objects that you might want to use throughout the whole app.
public class myClass {
private static int myInt;
public static void setMyInt(int i)
{
myInt = i;
}
public static int getMyInt()
{
return myInt;
}
}
Then you can set any variable from anywhere and use it anywhere.
myClass.setMyInt(10);
int i = myClass.getMyInt();
You may want to create your class MyAppication extends Application (you will also have to add it into Manifest). This MyApplication class will exist all the time your app is running so you can store things in there.
I think it would be better to use fragments for your purpose..
You have the main activity which has the tabhost/tabwidget and a layout to display a fragment for each..
Based on the currently selected tab you display the corresponding fragment in the layout.
This way your main activity can have whatever variables data your fragments need to use.
All the fragments are able to access the data in the activity they are displayed in, just make sure to create getters and setters

How do I create common code for parts of Android activities?

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers

Categories

Resources