The Activity Lifecycle is giving me headaches.
The documentation at http://developer.android.com/reference/android/app/Activity.html is so darn ambiguous when it describes the concept of visibility, that I can't figure out when onStop()
is called vs onPause()
.
Compare the following two statements from the documentation:
(taken from right beneath the lifecycle diagram)
The
onStart()
andonStop()
methods can be called multiple times, as the activity becomes visible and hidden to the user.
vs
(further down in the blue table with the "killable" columns)
onPause()
Called when the system is about to start resuming a previous activity.
What I'd understand from the first quote, is that onStop()
is called on activity A
when A
is "hidden". "Hidden" I'd guess is referring to when another activity B
has been resumed and is completely covering actvity A
.
But the second quote then states that onPause()
is called when another activity is about to start resuming. Wouldn't that completely hide activity A as well? Both cases seem to imply that that activity A
becomes "hidden", no? According to my likely faulty interpretation, onPause()
and onStop()
are called in identical situations.
The documentation also seems to differ between being hidden (onStop()
gets called) and being partial visibility (onPause()
gets called). But when is an activity still partially visible? Do they mean literally? Or can an activity still be deemed "partially visible" when it has started up a new activity (activity calls startActivityForResult and starts a date picker activity) that covers the entire screen? Surely the activity is not going get onStop invoked? Its supposed to receive a result any moment!
So I'm trying to figure out what I'm not getting.
I understand that a call to onPause is guaranteed. That would be when activity A
loses focus (device enters sleep mode, screenlock, etc), a different activity B
takes the foreground (where activity B
may or may not have been initiated by activity A
).
But at which point is the onStop()
invoked on activity A
?
Is it matter of how many activities have been piled ontop of activity A on the activity stack? Are there two different definitions of "visiblity" at play?
Sorry about the wall of text, but I'm really frustrated :S
So the question stands: Precisely in which situations is an activity deemed "hidden" such that onStop()
is called on it?
EDIT:
I inserted Toast notifications in each onX method, and discovered some additional weirdness:
- Pressing the Home button will always call onStop(). But starting up the application won't call
onRestart()
. Instead it callsonCreate()
. This seems strange to me, but ok... - When the "USB Mass Storage" activity is started on top of the main activity,
onStop()
is called. And when exiting the usb storage activity, returning to the main activity,onRestart()
is called, instead ofonCreate()
. - When the device goes into Sleep mode and is waken up, the activity only goes through the
onPause()
andonResume()
cycle.
The last point was expected (although I can't get it to fit in the lifecycle diagram). But whats up with 1. and 2. ?
In the first point, I was expecting a call to onRestart()
when starting the activity again. Why did it deallocate the activity and call onCreate()
instead?
And take a look at point nr 2:
According to the documentation: when "another activity comes in front of the activity", onPaused()
should be called. Isn't that what happened when the USB Storage activity came up? It didn't call onPause()
, it went through the onStop()
- OnRestart()
cycle! Obviously, the documentation doesn't consider that a case where "another activity comes in front of the activity". So what really happened?