import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
String tag = "LifeCycleEvents";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(tag, "In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}
In android applications, activities are a fundamental building block and they can exist in a number of different states. The lifecycle of an android activity begins with its instantiation and ends with destruction, and it includes many states in between. When an activity changes state, the appropriate lifecycle event method is called, notifying the activity of the impending state change and allowing it to execute code in order to adapt to that change.
Activity States
The Android OS uses a priority queue to assist in managing activities running on the device. Based on the state, a particular Android activity is in, it will be assigned a certain priority within the OS. This priority system helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an activity can go through, during its lifetime:

Activity Lifecycle Methods
As mentioned above, when an activity state is changed, the appropriate lifecycle method is called to impend the state changes and to allow it to execute the code in order to adopt that state change. The various callback methods associated with an activity life cycle and their relation with the activity states are shown below:

The following example illustrates the various lifecycle methods:
It was very useful using your log commands to see when exactly the lifecycle methods are being used. Something interesting you can try to having other activities in cover your current activity, or having them visible, but not in the foreground. Or perhaps being visible but not in the foreground, and then rotating the device.
There are a lot of different combinations to consider when analyzing these different life cycle methods. I found it fun to try to predict what methods would be called when I was going to perform a certain task.
For example, which methods would be called if a user starts the activity, starts using it, then switches to another app?
The answer would be:
onCreate -> onStart -> onResume-> onPause -> onStop
See if you can figure this one out!
User starts activity, starts using it, rotates the device, switched to another app, then goes back to that activity.
This one is tough!
If you think you got the answer feel free to send me an email to
erick@androidsyndicate.com
Also if your interested an example Stopwatch project that goes over activity life cycles, visit my blog at http://www.androidsyndicate.com
Happy coding everyone! 🙂
Erick Shaffer