Communication between fragments on Android

I am trying to create an Android application with two tabs, one for the textField / and TreeMenu buttons (where each element has a checkbox associated with it), and the other for the list. I also use ActionBarSherlock. I have already successfully written the program in one main action, but it’s hard for me to figure out how to split this initial action to fit the two new fragments that I need to create for each tab. Moreover, every time an element is added to the first tab (regardless of whether it is disabled or added to textField), the list in the second window should recognize the update.

To create an action bar, I can do this ...

ActionBar actionbar = getSupportActionBar(); actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionbar.setTitle("AppName"); 

To create tabs ..

  ActionBar.Tab Frag1Tab = actionbar.newTab().setText("InputTab"); ActionBar.Tab Frag2Tab = actionbar.newTab().setText("ListTab"); 

To create fragments and their listeners, the basis of each tab ...

 Fragment Fragment1 = new Fragment_1(); Fragment Fragment2 = new Fragment_2(); Frag1Tab.setTabListener(new MyTabsListener(Fragment1)); Frag2Tab.setTabListener(new MyTabsListener(Fragment2)); 

To add them to the action bar ...

 actionbar.addTab(Frag1Tab); actionbar.addTab(Frag2Tab); 

All this appears in my MainActivity. For example, I want the ArrayList variable to be available for both fragments, so, as I said, I can update the list. I would love to hear any help you can provide. I would be interested to see how the Otto API can work for something like this, but I'm not picky!

+2
source share
5 answers

The singleton class can help solve your problem.

 public class GlobalApp { private static GlobalApp instance = new GlobalApp(); private GlobalApp() {} public static GlobalApp getInstance() { return instance; } public ArrayList < ClassName > varName = new ArrayList < ClassName > (); } 

Then use in your class like this.

 GlobalApp.getInstance().varName 
+1
source

Some answers have a suggestion to put an ArrayList in a singleton, but does this really not solve your problem? Of course, you can access it wherever you are, but it will not help you save two different fragments using synchronization using arraylist.

Start by placing the ArrayList in singleton mode, as suggested, or in your MainActivity. Once this is done, you have at least two options for keeping the fragments in sync with the contents of the ArrayList:

  • Make ArrayList Observable (check ObservableArrayList ) and let Fragments Observe ArrayList for changes.

  • Use an event bus (e.g. Otto ), and let singleton or MainActivity (depending on where you put the ArrayList) report update events on the bus when the arrailist changes and Snippets subscribe to events.

+2
source

I had the same problem several times with ArrayList and other data. In the end, I decided to create a class that extends the application and which contains all the global data and can be accessed everywhere from the application. Do you want an example?

+1
source

As John said, when you want to access data in several actions, just create a class that extends Application:

 import android.app.Application; /** * Application class used to share data between activities. * @author Longeanie Christophe * */ public class MyApplication extends Application { //Put your class members here @Override public void onCreate() { //initialize what needed here } //Getters and setters... } 

In all your actions, you can access this class and its data using this instruction:

 MyApplication myApp = (MyApplication) getApplication(); 
+1
source

Here you go: first make sure you declare it in your AndroidManifest:

 <application android:name="com.example.main.ApplicationClass" etc..> <!-- other activities, services etc: --> </application> 

and your class:

 public class ApplicationClass extends Application { private static ApplicationClass THIS = null; ArrayList<String> list = new ArrayList<String>(); public void onCreate() { super.onCreate(); THIS = this; } public static ApplicationClass getThisInstance() { return THIS; } } 
0
source

Source: https://habr.com/ru/post/1487044/


All Articles