Activity Life cycle of Android

Activities in the Android are managed as an Activity stack. When an activity is started, it is placed at the top of the stack and becomes the running activity. The previous activity always remains below in the stack, and will not come to the foreground again until the new activity exists.

In the activity life cycle there are following methods:




onCreate():
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

onStart():
Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume():
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause().

onPause():
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

onStop():
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

onRestart():
Called after your activity has been stopped, prior to it being started again. Always followed by onStart()

onDestroy():
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Here is sample activity to understand activity life cycle:

package com.example.lifecycle;

import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "onDestroy()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "onPause()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(this, "onRestart()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "onStart()", Toast.LENGTH_SHORT).show();
}

@Override

protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "onStop()", Toast.LENGTH_SHORT).show();
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Run application and see the output.

First time Toast will be appeared:
onCreate()
onStart()
onResume()

After when you press home button, Toast will be appeared:
onPause()
onStop()

And when you again run your app from emulator, Toast will be appeared:
onRestart()
onStart()
onResume()

Here is zip file:  LifeCycle.rar

Comments

  1. Thank you seravina for like this post. I appreciate your feedback. I will keep updating my blog. Please subscribe to blog, if you want to get new posts via email. Thank you

    ReplyDelete

Post a Comment

Popular posts from this blog

SQLiteDatabase (Without SqliteOpenHelper class)

Set current date to Button and Datepicker dialog

MVC Architecture in Android