Object Sharing Between Activities in Android

天大地大妈咪最大 提交于 2019-12-06 01:42:52

问题


Hi I have a question about passing an object throughout the app. Say I want to have a big custom object throughout the app. This object will be used by multiple activities and services. What I did at first is this.

First, I created an Application class and define a singleton object.

public class FooApplication extends Application {
    public static SharedObj obj;
    ...
}

Second, in an activity I set a value of this object

public class FooActivity extends Activity {
    OnCreate() {
        FooApplication.obj = SharedObj.getInstance();
    }
}

Third, in another activity I access this object

public class BarActivity extends Activity {
    OnCreate() {
        SharedObj useThis = FooApplication.obj;
    }
}

My question is, what is wrong with this? It seems to work fine but I find that sometimes the value of this singleton object is set to null for some reason. The primary reason I am sharing this object like this instead of making it parcelable is that doing so is kinda expensive for me and what I did looks much easy to do. Is there a downside?

After some research I found there is another way to share an object throughout the app.

First, define in app class

public class FooApplication extends Application {
    public SharedObj obj = new SharedObj();
    ...
}

Second, initialize in an activity like this

public class FooActivity extends Activity {
    OnCreate() {
        FooApplication fooApp = (FooApplication)getApplicationContext();
        fooApp.obj = new SharedObj();
    }
}

Third, in another activity I access this object

public class BarActivity extends Activity {
    OnCreate() {
        SharedObj useThis = ((FooApplication)getApplicationContext()).obj;
    }
}

How is this method (using getapplicationContext()) different from using a singleton object like I did in the first section? Is this more recommended?

Thanks in advance!


回答1:


The main downside to this approach is the fact that the Android system may kill your process at any point in time. Typically this is done when memory is needed for other purposes (like an incoming phone call for example). Android will then re-start your process, and re-create each activity (when the activity becomes visible). In doing so it will call each activity's onCreate method, passing it a bundle of the parcelized data that the activity has saved.

The application class will be re-created as well. The object you have saved there however will not be re-created.

Thus, as painful as it is, parcelization ensures that your object can be resorted to the state it was in when the activity (and the process) was killed.

Another approach would be to persist the object yourself. Using SharedPreferences is one common approach.

====

So in summary, I suggest passing the object from activity to activity via Intent. The intent that is used to launch an activity is always fully re-constituted when that activity is re-created, even in the case of a process kill/re-launch.



来源:https://stackoverflow.com/questions/28219787/object-sharing-between-activities-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!