1

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 method. However, I couldn't find any similar way for a service.

I can use static object but it's unsafe as there is no guaranty, if the reference would be valid the next time, the service is created.

Another possible way would be, I store all the data on storage & later on when the service is created again, I can read the data from storage. Then traverse through information/data provided by the another class & find the relevant reference for the info read from the storage, which I think is very expensive interms of execution time.

So I am looking for a way to get rid of all this process & keep a strong reference (of the object in service) in the memory as long as the task/app is running.

Regards

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • Why don't you try using application class? That will persist data during the app running time. Some thing like this http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – Mohan Krishna Aug 11 '14 at 06:57
  • Why don't you like to use static references? you could just re-initialize them if needed... They keep living as long as the process does, just like the Application class. – android developer Aug 11 '14 at 11:55
  • @androiddeveloper From my experience, static variables are destroyed under critical memory conditions (even when the task is alive but its process or service is killed). I will test Application class as an alternative to static objects – Umer Farooq Aug 11 '14 at 14:03
  • @UmerFarooq if the process is killed, the task and service are killed. everything of the app is killed. only remaining thing could be that you can re-open the app via the recent-tasks. And it is correct that it can happen on low memory scenarios, such as when you open a memory-intensive app. Extending the Application is equivalent to using a static reference (in terms of storing a variable), as it will live as long as the process lives - it's instantiated when the process is. The only difference is that it won't work well when creating a contentProvider (which won't create an instance of it). – android developer Aug 11 '14 at 14:20
  • @UmerFarooq You can even read about how the Application class works (as I've written) here: http://developer.android.com/reference/android/app/Application.html#onCreate() and here: http://developer.android.com/reference/android/app/ . They say you don't usually need to use this class if you need a singleTon, which also mean that you don't need it for a static reference too. – android developer Aug 11 '14 at 14:22
  • @androiddeveloper My scenario included several other services which kept running even when one of them is killed. So the task will keep running, it won't die until all children/processes of the tasks die. BTW one of the links you provided is dead – Umer Farooq Aug 12 '14 at 15:40
  • @UmerFarooq About the link, this is the correct one: http://developer.android.com/reference/android/app/Application.html . about your scenario, I don't understand the problem : why would you assume that when a service is destroyed, others will be destroyed too ("kept running even when one of them is killed") ? Also, if you have services, why do you care so much about the task? and what are the "children" that you are talking about? Anyway, the process will live as long as the OS says it will, and that's based on the status of the various app's components. – android developer Aug 12 '14 at 16:14

1 Answers1

0

You can use application class to persist data as

public class MyApplication extends Application 
{     
     public MyDataClass data = new MyDataClass();
}

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::

<application android: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.

  • saving to the application class is usually the same as saving to a static variable. – android developer Aug 11 '14 at 07:38
  • 1
    No its not. Static variable will be reclaimed in case of memory is needed but application variable will be intact to application life cycle. – Devendra B. Singh Aug 11 '14 at 07:45
  • Do you have proof about this? I've already asked about this exact same difference, here: http://stackoverflow.com/a/19866181/878126 . I even remember the user "CommonsWare" telling me somewhere that the static variable should remain as long as the process is alive. – android developer Aug 11 '14 at 11:33
  • Also, one reason it's ok is that the documentation say it's ok to share data between activities using a static variable (or a singleTon, which obviously uses a static reference) : http://developer.android.com/guide/faq/framework.html#3 . there are other uses to have the application class, but you really don't have to put variables there if you just want to use them globally and as long as the process lives. – android developer Aug 11 '14 at 11:42
  • Do you have any alternative solution than please share. – Devendra B. Singh Aug 11 '14 at 11:46
  • @DevendraB.Singh Did you find any alternative approach ? One thing we can serialise object and keep it in shared preferences. – Brajesh Kumar Jul 28 '15 at 20:10