I am attempting to build an Android application that has two tabs, one for a textField/button and TreeMenu (where each element has a checkbox associated with it) and another for a list. I am also using an ActionBarSherlock. I have already successfully written the program in a single main activity, but am having difficulty figuring out how to divide this original activity to correspond with the two new fragment instances I need to create for each tab. Moreover, each time an item is added in the first tab (whether it is from being checked off or added to the textField), the list in the second window ought to recognize the update.
To build the action bar I can do this...
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("AppName");
To create the tabs..
ActionBar.Tab Frag1Tab = actionbar.newTab().setText("InputTab");
ActionBar.Tab Frag2Tab = actionbar.newTab().setText("ListTab");
To create the fragments and its listeners underlying 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 of this appears within my MainActivity. I want, for example, an ArrayList variable to be accessible to both Fragments so, like 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 could work for something like this, but I am not picky!
As John said, when you want to access data in several activities, just create a class that extends Application:
In all your activities, you can access this class and its data through this instruction:
In some of the answers there's a suggestion to put the ArrayList in a singleton, but that doesn't really solve your problem does it? Sure, you'll be able to access it from where ever you like, but it doesn't help you keeping two different fragments, both using the arraylist, in sync.
Start by putting the ArrayList in a singleton as suggested or in your MainActivity. Once that is done you have at least two options for keeping the fragments in sync with the content of the ArrayList:
Make the ArrayList Observable (check ObservableArrayList) and let the Fragments Observe the ArrayList for changes.
Make use of an event bus (like Otto) and let the singleton or MainActivity (depending on where you put the ArrayList) post update events on the bus when the arraylist changes and have the Fragments subscribe to the events.
A singleton class could help solve your problem.
Then use in your class like this..
I had the same issue a little time ago with an ArrayList and other data. In the end I decided to create a class that extends application and that holds all the global data and can be accessed everywhere from the app. Do you want and example?
Here you go: first be sure you declare it in your AndroidManifest:
and your class: