Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android 'kill' an Activity without killing the application?

As we know, the default flow in Android for such scenario is calling the activity's respective onSaveInstanceState, onStop, onDestroy methods before releasing the reference to the Activity object.

However it appears I have a case when my application is on the background, the activity gets killed without those methods being called, but my application itself does not get destroyed.

However I am unable to force-reproduce this. Whenever I use applications on the foreground that require a lot of resources, the whole process gets killed, not just the activity.

Which kind of makes me wonder, because I believe the 'app killing' on low resources is essentially just the old signal way, does the Android system actually 'kill' (release) an activity instantly without calling these methods? Or am I chasing ghosts?

like image 445
George Avatar asked Aug 05 '13 08:08

George


2 Answers

It is possible Android OS kills only some of your activities even if your app is in foreground. For instance if you have two activities A and B, and when A calls startActivity / startActivityForResult to start activity B then Android may deceide to destroy activity A's instance because it is taking up too much memory space.

You can force killing activities which don't run in the foreground by checking Don't keep activities in developer options menu.

like image 74
maRci002 Avatar answered Oct 16 '22 14:10

maRci002


Android app out of memory issues - tried everything and still at a loss

This is not how things work. The only memory management that impacts activity lifecycle is the global memory across all processes, as Android decides that it is running low on memory and so need to kill background processes to get some back

Now the explanation in the official documents is more clear

The system never kills an activity directly to free up memory. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well. To learn how to preserve and restore your activity's UI state when system-initiated process death occurs, see Saving and restoring activity state.

like image 3
CharlesYale Avatar answered Oct 16 '22 14:10

CharlesYale