简体   繁体   中英

Preferred way for multiple screens to share data in an Android app?

I have an Android application that needs to access data from different screens. The main problem is, each one needs to be able to modify the data, and there is quite a bit of it - 3 million integers divided among six arrays.

The options that I can think of are:

  1. Put each screen in a separate view, and use a ViewAnimator as the topmost layout, switching between the child views as necessary; this way, the arrays are defined in the main activity and are accessible (both reading and writing) by all of the screens.

  2. Put each screen in its own Activity, and pass the data from one screen to another by putting it into a Bundle that is included in the startActivityForResult call (and any changes would be passed back through the returned Intent's Extras in onActivityResult). The problem with this is, the data has to be copied quite a few times, which, it seems to be, involves quite a bit of overhead in terms of both time and memory.

  3. Create a Class to hold the data; create a variable of that Class in the main activity, and pass that variable back and forth - but is the variable itself being passed to the second activity, or a copy of it (so any changes would then not be reflected in the variable in the main activity)?

Is there a preferred method for a situation like this? Am I leaving something out?

Your option #3 is headed in the right direction. As far as accessing the variable from anywhere.. you want to use a Singleton design pattern. Generally this creates a single instance of the resource (your 3 million integers in this case!) If each Activity is working on a different 'set' of data then I have misunderstood your request and you should not use this singleton approach.

I've used a String (mExamplePayload) to represent your data for the sake of a simplified example. For your specific implementation you could create a single variable which contains each array (or create a variable for each array.)

public class MyData {

    private static MyData sMyData;
    private Context mAppContext;
    private String mExamplePayload;

    private MyData(Context c){
        mAppContext = c.getApplicationContext();
        mExamplePayload = "You can access this string from any Activity or Fragment";
    }

    public static MyData get(Context c){
        if (sMyData==null){
            sMyData = new MyData(c);                
        }
        return sMyData;
    }

    public String getPayload() {
        return mExamplePayload;
    }   
}

And this is how you'd access the data within your Activity

MyData data = MyData.get(this);
String test = data.getPayload();

Or Fragment

MyData data = MyData.get(getActivity());
String test = data.getPayload();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM