Understand the Activity Lifecycle | Android Studio Tutorials

Understand the Activity Lifecycle | Android Studio Tutorials

android lifecycle

Android activity life cycle is controlled by following methods. 
onCreate()onStart()onResume()onPause()onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.

onCreate Called when activity is first created.

onStart     Called when activity is becoming visible to the user.

onResume     Called when activity will start interacting with the user.

onPause :    Called when activity is not visible to the user.

onStop :      Called when activity is no longer visible to the user.

onDestroy Called before the activity is destroyed.

onRestart : Called after your activity is stopped, prior to start.

A visual representation of this paradigm : 

You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.

Example - 

TextView textView;

// some transient state for the activity instance
String gameState;

public void onCreate(Bundle savedInstanceState) {
// call the super class onCreate to complete the creation of activity like
// the view hierarchy

// recovering the instance state
if (savedInstanceState != null) {
= savedInstanceState.getString(GAME_STATE_KEY);

// set the user interface layout for this activity
// the layout file is defined in the project res/layout/main_activity.xml file

// initialize member TextView so we can manipulate it later
= (TextView) findViewById(R.id.text_view);

// This callback is called only when there is a saved instance that is previously saved by using
// onSaveInstanceState(). We restore some state in onCreate(), while we can optionally restore
// other state here, possibly usable after onStart() has completed.
// The savedInstanceState Bundle is same as the one used in onCreate().
public void onRestoreInstanceState(Bundle savedInstanceState) {

// invoked when the activity may be temporarily destroyed, save the instance state here
public void onSaveInstanceState(Bundle outState) {
.putString(GAME_STATE_KEY, gameState);
.putString(TEXT_VIEW_KEY, textView.getText());

// call superclass to save any view hierarchy

When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.

When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode). Use the onPause() method to pause or adjust operations that should not continue (or should continue in moderation) while the Activity is in the Paused state, and that you expect to resume shortly.

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running, and is about to be terminated.

Example - 

protected void onStop() {
// call the superclass method first

// save the note's current draft, because the activity is stopping
// and we want to be sure the current note progress isn't lost.
ContentValues values = new ContentValues();
.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());

// do this update in background on an AsyncQueryHandler or equivalent
.startUpdate (
,  // int token to correlate calls
null,    // cookie, not used here
,    // The URI for the note to update.
,  // The map of column names and new values to apply to them.
null,    // No SELECT criteria are used.
null     // No WHERE columns are used.

onDestroy() is called before the activity is destroyed. The system invokes this callback either because:

  1. the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
  2. the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

Feel free to ask your doubts in comment section.

We're always open to suggestions.

Post a Comment