Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion over the Android Activity Lifecycle

I have an app which is one activity.

Everything works as I would expect except when I put the phone (a Samsung Galaxy Ace running Gingerbread) to sleep with the button on the side. When I do this, the following are called (in this order):

onPause, onStop, onDestroy, onCreate, onStart, onResume, onPause.

This is without waking the phone up, it is still asleep - screen is off.

Why is the activity killed completely and re-recreated?

Even more bizarre, if the phone is then switched back on the following happens:

onResume, onPause, onStop, onDestroyed, onCreate, onStart, onResume

Even though it's 'ready to go' (sitting in onPause), it gets killed and re-created all over again.

If the app is running and I press Home then the activity is only stopped, not killed, once stopped, I can then put the phone to sleep and then wake it up again and the activity is never killed. It just goes to onRestart etc... which is what I would expect.

This behaviour only occurs when the phone is put to sleep while the activity is running (and in the foreground).

I've read the docs on the Activity Lifecycle but I can't figure out why this behaviour occurs.

like image 772
Zippy Avatar asked Feb 16 '23 03:02

Zippy


1 Answers

I have the same behaviour on other Samsung device when the Activity is forced to be shown in landscape (set in AndroidManifest.xml).

This happens because the device lock screen is forced to be shown in portrait and while it is in front of your application, it forces your top level Activity into portrait.

This is nothing to worry about and also nothing you can change.

like image 199
MaciejGórski Avatar answered Feb 27 '23 07:02

MaciejGórski