Issue
I can't find information on where am I in first activity when I hit "back" in the second. Why?
The first one surely is not being re-made, so in which "on..." (like onResume(), onStop(), etc.) am I?
Solution
The answer depends on your activities. Here are a couple of different scenarios that will result in different life cycle methods being called. Every scenario below assumes there is an activity A
and B
, where the latter is invoked by the first; i.e. after a button press.
Scenario 1: B
is translucent and/or not full size (i.e. dialog style)
Activity A
will lose focus but is still visible because B
is non-full-sized and/or transparent and sits on top of A
. In this case A
will be paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
Scenario 2: B
is full size
Activity A
is completely obscured by B
. It will be stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
All of the above is well-documented in the life cycle section of the Activity
class.
So we now know that activity A
will be either paused (scenario 1) or stopped (scenario 2), or it will have been killed by the system to free up memory. Let's first assume that the latter doesn't happen, then the following life cycle methods are relevant:
Scenario 1: onPause()
when B
shows, followed by onResume()
when A
comes back into focus.
Scenario 2:: onPause()
and onStop()
when B
shows, followed by onStart()
and onResume()
when A
comes back into focus.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state. That means the activity will start again with onCreate()
, followed by onStart()
and onResume()
.
In the life cycle documentation you'll find the following flow diagram that you may find of use:
Answered By - MH.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.