Friday 14 September 2012

Android Activity Life Cycle

The most important concept for every application in Android is Activity. Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity, the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

Basically an activity has four states.
1. Running
2. Paused
3. Stop
4. Destroy

Running:
                  If an activity in the foreground of the screen (at the top of the stack), it is active or running. This means that you are using this activity now. That's why it is in foreground. 

 Paused:   
                     If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is 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.

      Stop: 
                            If an activity is completely obscured by another activity, it is 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.

     Destroy: 
                     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.

        The Bellow picture tells about this life cycle:


         onCreate() : 
                                   When first time loading your activity, onCreate() will be called.

       onStart():
                                 Your activity already loaded previously, now again you are opening this activity, on that time onStart() will be called.

       onResume():
                                This means that your activity is in the foreground on the screen.

       onPause():
                                If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is 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.   
        onStop(): 
                                         This will be Called when the activity is no longer visible to the user.

      onDestroy() :
                               This is the final call when your activity finishing it's operation. 

No comments:

Post a Comment