0

i have a Object obj where i need to save the state of this object for long time, so if i extend application class what is the Lifetime of it?

When the user in the current activity and gets a call, after long time the user resumes the activity what will happen to my object and the activity?

Goofy
  • 6,098
  • 17
  • 90
  • 156

1 Answers1

2

Your app's application instance will persist as long as Android allows it to. Whenever the system needs more memory for the foreground or high priority tasks, it will kill your application, including any activity, service and application instances.

There is no set time for which this object will be around. You must write your application assuming that it can be destroyed at any point of time.

You can override onLowMemory() in the Application class to get a rough idea of whether or not you may be killed soon.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • ok what so if i initialise the object by extending the Application class and the activity is in background for 2 to 3 hrs what will happen? – Goofy Feb 15 '13 at 13:46
  • @Goofy Your app could not be killed for a year if no one is using the device, or it may be killed within seconds of being closed if the user starts a memory intensive game right after. There is really no way to predict what will happen to your app in 2-3 hours. – Raghav Sood Feb 15 '13 at 13:47
  • 1
    @Goofy Yes, it would be better to implement serializable or pareceable and saved the object to disk. – Raghav Sood Feb 15 '13 at 13:51
  • i have one more issue can you please help me on that? – Goofy Feb 15 '13 at 13:51
  • http://stackoverflow.com/questions/14894429/reading-a-class-in-parcelable please have a look here – Goofy Feb 15 '13 at 13:52
  • @Goofy If it is unrelated to the question, you should open a new question for it – Raghav Sood Feb 15 '13 at 13:52
  • i have already opened it, please check this http://stackoverflow.com/questions/14894429/reading-a-class-in-parcelable – Goofy Feb 15 '13 at 13:52
  • @Goofy Can't help you with that, sorry. – Raghav Sood Feb 15 '13 at 13:56