简体   繁体   English

多个屏幕在Android应用中共享数据的首选方式?

[英]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. 我有一个Android应用程序,需要从不同的屏幕访问数据。 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. 主要问题是,每个数据都需要能够修改数据,并且其中有相当一部分-将300万个整数分配在六个数组中。

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; 将每个屏幕放在单独的视图中,并使用ViewAnimator作为最顶层的布局,并在必要时在子视图之间切换; 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). 将每个屏幕放在其自己的Activity中,并将数据从一个屏幕传递到另一个屏幕,方法是将其放入startActivityForResult调用中包含的Bundle中(所有更改都将通过onActivityResult中返回的Intent的Extras传递回去)。 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; 创建一个Class来保存数据; 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)? 在主要活动中创建该Class的变量,并来回传递该变量-但是变量本身是传递给第二个活动还是它的副本(因此,任何更改都不会反映在主要活动)?

Is there a preferred method for a situation like this? 对于这种情况,有没有首选的方法? Am I leaving something out? 我要丢东西了吗?

Your option #3 is headed in the right direction. 您的选择#3朝着正确的方向前进。 As far as accessing the variable from anywhere.. you want to use a Singleton design pattern. 至于从任何地方访问变量,您都希望使用Singleton设计模式。 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. 通常,这将创建资源的单个实例(在这种情况下为您的300万个整数!)如果每个Activity都在处理不同的“数据集”,那么我会误解了您的请求,因此您不应使用这种单例方法。

I've used a String (mExamplePayload) to represent your data for the sake of a simplified example. 为了简化示例,我使用了一个String(mExamplePayload)来表示您的数据。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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