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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With