Communicative Between Fragments on Android

2020-04-14 08:56发布

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!

5条回答
趁早两清
2楼-- · 2020-04-14 09:14

As John said, when you want to access data in several activities, 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's needed here
    }

    //Getters and setters...
}

In all your activities, you can access this class and its data through this instruction:

MyApplication myApp = (MyApplication) getApplication();
查看更多
Animai°情兽
3楼-- · 2020-04-14 09:20

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:

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

  2. 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.

查看更多
forever°为你锁心
4楼-- · 2020-04-14 09:28

A singleton class could 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
查看更多
来,给爷笑一个
5楼-- · 2020-04-14 09:33

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?

查看更多
祖国的老花朵
6楼-- · 2020-04-14 09:34

Here you go: first be 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;
}
}
查看更多
登录 后发表回答