Android Activity Lifecycle
An Activity in Android can exist in four states :
Active/Running state :
"When an activity is in the front and has focus in it. It is completely visible and active to the user."
Paused state :
"The activity is partially visible to the user but not active and lost focus. A paused activity is completely alive and maintains its state but it can
be killed by system under low memory when memory can be freed by no
other ways."
Activity is on top of this one which does not cover the entire screen.
Stopped state :
"It's when Activity is no longer visible in the screen. The activity is alive and preserves its state, but more likely to be killed by the system to free resources whenever necessary."
Another activity is on top of it and completely.
Destroyed/Dead state :
"Either the Activity killed by the system in Paused or Stopped state to free resources."
Android Activity Lifecycle Diagram :
- When we launch an Activity in Android, it first calls the onCreate()
method. This is where we do User Interface creation and initialization
of data elements. This method is provided with a Bundle object as
parameter to restore the UI state.
- onStart() method is called before the Activity is being visible to the User. Remember that Activity is still not Active.
- onResume() method, the Activity become
visible and Active for the user to interact with. The Activity will be
at the top of the Activity stack at this point. Now the Activity is in
running /active state and is able to receive user inputs.
Activities should override this method to perform tasks such as:
- Ramping up frame rates (a common task in game building)
- Starting animations
- Listening for GPS updates
- onPause() method will be
called when the system is about to resume another Activity on top of
this one or when the user is about to navigate to some other other parts
of the system. It is the last guaranteed call to a method before the
Activity can get killed by the system. That is, there’s a possibility
that your activity may be killed by the system at the paused state
without executing any further method calls. Therefore it is important to
save the user interface configuration and critical data at this method.
- By default, an Activity can remain in the paused state if:
- The user has pressed the home button
- Another activity or notification which is on top of it doesn’t completely obscures the visibility of underlying Activity.
- The device goes to sleep.
- There are three possibilities for an Activity under paused state:
- The user resumes the Activity by closing the new Activity or
notification and the paused Activity gets Active/Running by calling onResume() method.
- It gets killed by the system under extremely low memory conditions.
In this case there will be no further method calls before the
destruction of the Activity and it needs to be re-run from the beginning
by calling onCreate() and restoring the previous configuration from bundle object.
In all other cases it goes to stopped state by executing onStop()
method. This is the default action when the user has pressed the back
button.
- onStop() called when you are no longer visible to the user. You will next receive
either onRestart(), onDestroy(), or nothing, depending on later user
activity.
Note : This method may never be called, in low memory situations
where the system does not have enough memory to keep your activity’s
process running after its onPause() method is called.
An Activity under stopped state also has three different scenarios to happen :
- System kills it to free resources. An activity under stopped sate is
more likely to be killed by the system than one in the paused state. It
needs to start the cycle again with onCreate().
- It get restarted by calling onRestart() , onStart() and onResume()
methods in the order if the user navigates back to the Activity again.
In this case, the User Interface is intact and no need to be restored.
- onDestroy() method is called and the Activity is
destroyed. This is the final method we can call before the Activity is
destroyed. This occurs either because the Activity is finishing the
operation or the system is temporarily destroying it to save space.
No comments:
Post a Comment