Tuesday, 15 March 2011

Android activity life cycle - From onCreate() to onDestroy()

The activity is one of the most important classes in Android. It has direct classes such as ListActivity or AccountAuthenticatorActivity, and indirect classes such as LauncherActivity and TabActivity.


Here you can see the schema about the lifecycle of an activity.
There are five states for an activity
  • Running: When the application is in the foreground and the user can see it completely. In this case the activity is put in the first place in the stack.
  • Paused: When another activity come to the foreground. In this case, the applications are partially hidden. It can be killed in the extreme poor memory state.
  • Stopped: The activity is not longer visible but the activity is still running. For example the music player can still playing music when it is not visible. The activity could be killed by the system.
  • Killed: When an activity is paused or stopped, it can be killed. If the user want to see it again, it will start again from the beginning (will the state saved)
  • Destroyed: When the activity is not longer running anymore. All the resources of the activity will be released.
Notice there are three key loops here:
  • The entire lifecycle: From onCreate() to onDestroy(). In onCreate() the activity creates all the resources that it may need and it should release all them in onDestroy()
  • The visible lifecycle: From onStart() to onStop(). Here is when the activity remains visible. The activity should be prepared for the user to interact with it.
  • The foreground lifecycle: From onResume() to onPause(). In other word, during the running state. This lifecycle could be called several times, that´s why it should be lightweight.

And this is the basic code structure of an activity in Android:

public class Activity extends ApplicationContext {
    protected void onCreate (Bundle savedInstanceState) {}

    protected void onStart(){ 

        super.onStart()
    }

    protected void onRestart(){

        super.onRetart()

    }


    protected void onResume(){
        super.onResume()

    }


    protected void onPause(){
        super.onPause()

    }


    protected void onStop(){
        super.onStop()

    }


    protected void onDestroy(){
        super.onDestroy()

    }
}

A short explanation:
  • onCreate(): Called when the activity is first created. This could be when the activity starts or when it comes from frozen state (when the activity was killed). Normally the setContentView(int) method is called here to set the UI.
  • onStart(): Called when the activity comes to the foreground and be visible for the user.
  • onResume():  The activity is prepared to interact with the user.
  • onPause(): Called when another activity comes to foreground. It is a good moment to commit all the changes to persistent data because after this moment the activity could be killed in any time.*
  • onStop(): When the activity is completely covered and there is not possibility to interact with it.
  • onRestart(): After the activity has been stopped, it could bring back to the foreground again. In this case, the activity will start again from onStart()
  • onDestroy(): The activity is completely destroyed. All the resources are released.
*The method onSaveInstanceState(Bundle) is called before the application is killed, then it is possible use the Bundle in the method onCreate(Bundle). Notice this method just save the dynamic instance state in the activity and not always will be called in every situation.


To be clarified: The official guide say that the activity could go from onStart() to onResume(). That means that before the activity goes to be visible, it was hidden. But it doesn´t appears on the schema.


Note: The activity will also be restarted when the configuration of the phone (language, screen orientation, input devices, etc). That means the activity will go to onPause() -> onStop() -> onDestroy() and restart again from onCreate() with a new instance. The previous instance will be retrieved in onCreate() method.

For more information, visit the android official developer´s guide:
http://developer.android.com/reference/android/app/Activity.html

No comments:

Post a Comment