Android Logo MathCS.org - Android

Life Cycle of an Activity

java -> android -> activity life cycle ...

So far we programmed "old-style": we created a single class that extended Activity, implemented the onCreate method, added a few widgets and event handlers and called the whole thing a program or application. The reality of programming for Android, however, is more complex:

Let's deal with the basic building block of an application or program: the Activity class. An activity has essentially four states:

Here are the four states in a diagram including the transition paths from one state to another (the image is taken from the Android SDK documentation).

activity states

Thus, an activity usually overrides the following methods to transition correctly between states:

Each of these methods should place a call to its superclass method. Thus, a complete framework for an activity should look like this (create a new project called FirstLife). We've added log calls to each method so we can see when it is called - those log calls would not be necessary in a real activity):

public class FirstLife extends Activity 
{
	private final static String DEBUG_TAG = "FirstLifeLog";
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		Log.i(DEBUG_TAG, "onCreate executes ...");
		setContentView(R.layout.main);
	}

	protected void onRestart()
	{
		super.onRestart();
	        Log.i(DEBUG_TAG, "onRestart executes ...");
	}
	
	protected void onStart()
	{
		super.onStart();
		Log.i(DEBUG_TAG, "onStart executes ...");
	}

	protected void onResume()
	{
		super.onResume();
		Log.i(DEBUG_TAG, "onResume executes ...");
	}

	protected void onPause()
	{
		super.onPause();
		Log.i(DEBUG_TAG, "onPause executes ...");
	}

	protected void onStop()
	{
		super.onStop();
		Log.i(DEBUG_TAG, "onStop executes ...");
	}    
	
	protected void onDestroy()
	{
		super.onDestroy();
		Log.i(DEBUG_TAG, "onDestroy executes ...");
	}
}

Now start the activity in debug mode and watch the LogCat output:

When the activity first starts:
app life cycle start
From activity start to pressing the device BACK button:
From activity start to pressing the device HOME button:
From activity start to pressing the device HOME button to long-click on HOME to bring activity back:
From activity start to interrupting it by pressing PHONE to returning by pressing BACK:

Now add a single EditText field to layout.xml to see what happens to user input as an activity goes through its cycles. It is best to install the activity on a real devices to see the impact of a screen orientation change.

  1. Start activity, type "Hello" into text field. Press HOME to go to the home screen, then long-click HOME and bring the activity back to the foreground: the text is still there
  2. Start activity and type "Hello" into the text field. Press BACK then restart the activity from the home screen: the text is gone
  3. Start activity and type "Hello". Rotate device to get the screen orientation to change. If you ran the activity in debug mode on a real device (make sure to set debug mode to true in the manifest file first) you'd see the same calls as pressing BACK and restarting the app, i.e. onDestroy is called: the text is still there, however

Next we will learn how to tie together multiple activities into one application, maintaining state data like user input when appropriate as activities call each other.