Android – Activity Life Cycle

Android activity has a different set of states. The whenever user navigates through application or does back and forth, the app Activity goes through different lifecycle states. The callback methods allow the activity to know all the changes in state. These callback methods tell if the system is creating, stopping or resuming activity.

In callback methods, you can define the behavior of Activity. If you are in-app and an incoming call comes, then your app should not crash. Lifecycle callback methods allow you to handle transition smoothly.

For example, properly handled lifecycle methods can help,

  • Crashing of the application if the user gets a phone call or switches to another app while using your app.
  • Consumption of system resources when the app is not in use
  • In keeping track of users activity, when they come back after time lapse.
  • It helps to store the data during screen orientation changes.

Activity class provides six core callback methods.

  • onCreate() – This a must implement callback. It is fired when Activity is created. This is invoked only once in the entire life cycle of activity. You can do setup activity in this callback. After the successful execution of onCreate(), activity enters into the start state
  • onStart() – After activity is created, system enters in start state where onStart() is invoked. The activity becomes visible to the user. Activity doesn’t stay long in this state. It moves to resumed state.
  • onResume() – After onStart(), activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback method. App’s interaction with user happens in this state. This state is changed whenever the user changes the activity, changes the app or receives a phone call. When interruption happens in this state, activity changes in the pause state.
  • onPause() – onPause() is invoked when user is leaving the activity. onPause() activity indicates that the activity is no longer in the foreground. onPause() activity can be used to release system resources.
  • onStop() – when activity is no longer visible to the user, it’s in stopped state and system invokes onStop() callback. onStop() can be called during the completion or termination of activity.
  • onDestory() – This is called just before the activity is destroyed. This calling method should release all held resources.

Sample.

TestActivity.java

public class TestActivity extends Activity {
String msg = "Android : ";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.d(msg, "The onCreate() callBack");
}

/** Called when the activity is about to become visible. */
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() callBack");
}

/** Called when the activity has become visible. */
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() callBack");
}

/** Called when transition happens. */
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() callBack");
}

/** Called when the activity is no longer visible. */
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() callBack");
}

/** Called just before the activity is destroyed. */
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() callBack");
}
}
Activity will load all UI component defined in activity_test.xml file.
TestActivity class is defined in AndroidManifest.xml. 


Leave a Reply

Your email address will not be published. Required fields are marked *