0

Android activities have a well defined lifecycle and they will be paused, stopped, and destroyed. That is very well documented. My question is about the task that contains my activities as described in the docs.

Under low memory or other conditions, will the task ever be shut down by the OS? (I understand the user can Force Stop the app).

Can I assume my Singletons and static data will always be available?

What about static members defined on an Activity? It appears that the Activity may be destroyed, but the static data lives as long as the task lives.

android.app.Application has callbacks like onLowMemory() and onTrimMemory() but these seem to be voluntary, meaning it is great if the app cooperates with the OS, but it doesn't have to. So, I'm pretty certain my task is never normally killed and singletons and statics are reliable. Is this correct?

This answer also had some good background.

Community
  • 1
  • 1
Michael Levy
  • 13,097
  • 15
  • 66
  • 100

3 Answers3

2

I was extremely curious about your question and so I quickly did a simple check with a very simple project and I'll post what I found - I'll answer the rest based on hypotheses as Android's clean up process is quite arcane.

  • Created 3 activities - A, B, C. A can call B or C. A is the starting point of the app. B and C cannot call anything. C has a static integer member x.
  • x is a class member of C and initially does not have any value. I set it to 5 and printed it out in the onCreate() of C. It is also printed out in the onResume() of A.
  • When I started the app, a toast was displayed on A which said 0 (C.x has not been set yet). Then I navigate to C. I get a toast c=5.
  • Then I press back and go back to A. I get a toast c=5. C has been destroyed.
  • Then I go to B. No toast. Go back to A. I get a toast c=5.
  • From A, I press home. From home, I again reopen the activity. I get c=5.
  • I press back to go back to home (destroying A). Then I reopen the app. I get c=5.
  • Then I go back and force stop the app from settings. Then I reopen the app - I get c=0. Force stop completely removes everything from this task.

From this, you can say that static values continue to exist even after the Activity has been destroyed. Even after an application is closed, the value is still retained by Android.

Can I assume my Singletons and static data will always be available?

I would say that you can assume this as long as Android is not pushed to the point where it decides that memory needs to be reclaimed.

From the Android tutorial book by Commonware, I understand that any task that is running in the background is considered as an important task and will not be shut down by Android. But if another task comes to the foreground, then this is given a higher priority and all tasks in the background assume lower priority. If Android decides the task in the foreground needs more memory, it will start killing processes based on order of priority which depends upon a number of factors. Under low memory conditions, any task can be killed. In extreme cases, the task on the foreground itself may be killed.

I hope this answers your question. It certainly piqued my interest.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • Do you have a link to the book you mention? And, it sounds like under extreme memory conditions, my task can be killed. If it gets killed, my statics and singletons are gone too. But I assume Application.onLowMemory() would be called so I can try to save state or shutdown gracefully. – Michael Levy Mar 18 '14 at 19:02
  • 1
    I remember specifically reading that the programmer should not rely on Android calling the callbacks when it is about to kill the activity - namely the onStop() and onDestroy(). Extending this, I am not sure if you can rely on it calling onLowMemory() but then again I have never experienced it. As for the book, you can just go to commonsware.com. It's actually a paid subscription - about $40 for a year. Updates come out every month. This is by far the most comprehensive guide that I have seen – ucsunil Mar 18 '14 at 19:12
0

Can I assume my Singletons and static data will always be available?

You can never assume that. Sometimes contex changes (when you go to the settings and turn gps on for instance) and then your apps is being recreated. OS can destroy your activity in any time. You should never operate on static data even on PC programs

Gaskoin
  • 2,469
  • 13
  • 22
  • Are you talking about Activities being destroyed or Tasks being destroyed? The question was about Tasks. – Michael Levy Mar 18 '14 at 19:29
  • It does not matter, static data is always fragile. OS will rather kill whole application than only one Activity. Singleton/static data is the worst thing you can do :) Better cache it in other way – Gaskoin Mar 18 '14 at 19:37
  • What are you basing this statement on "OS will rather kill whole application than only one Activity." ? – Michael Levy Mar 18 '14 at 19:49
0

It seems that the OS can kill your task in low memory and other situations. And, of course, your statics and singletons die with the task.

If you need your statics and singletons to survive task shutdown and restart, you need to persist them somewhere. Android Application Framework FAQ has good suggestions. I found that you can leverage onSaveInstanceState() and save your data as part of the save instance bundle. It will be restored when the Activity is restored. See Controlling Android Activity restart after a process stops

Community
  • 1
  • 1
Michael Levy
  • 13,097
  • 15
  • 66
  • 100