Skip to content Skip to sidebar Skip to footer

Keeping Reference Of An Object For A Task's Lifetime

I am looking for a way to keep reference of an object/instance that's a member object of a service. I know in case of Activities, we can save it by the provided onSaveInstance meth

Solution 1:

You can use application class to persist data as

publicclassMyApplicationextendsApplication 
{     
     publicMyDataClassdata=newMyDataClass();
}

Then call it in any activity/service by:

MyApplication appState = ((MyApplication)this.getApplication());
appState.data.useAGetterOrSetterHere(); // Do whatever you need to with the data here.

In your manifest mention it as follows::

<applicationandroid:name="yourpkgname.MyApplication"/>

More info are like

Application object is an object whose lifecycle is same as our Application.

When Application starts this object is created automatically by Android Framework.

When you want to share data between more than one activities of your application or you want to retain something at application level.In that scenario you can use Application object as a global store for your application.

Post a Comment for "Keeping Reference Of An Object For A Task's Lifetime"