Click here to Skip to main content
12,887,933 members (46,059 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 4 Sep 2013

Android: Activity Life Cycle

, 4 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Here we see how Android Activity LifeCycle works.


An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).


Activity is a window that contains the user interface of your application. As there are various states of activity like Running, Paused, Stopped and Killed.

Activity base class contains events that govern the life cycle of an activity.

  • onCreate(): Called when the activity is first created
  • onStart(): Called when the activity becomes visible to the user
  • onResume(): Called when the activity starts interacting with the user
  • onPause(): Called when the current activity is being paused and the previous activity is being resumed
  • onStop(): Called when the activity is no longer visible to the user
  • onDestroy(): Called before the activity is destroyed by the system
  • onRestart(): Called when the activity has been stopped and is restarting again

Using the Code

To create an activity, your class need to extends Activity base class. An Application has more than one activity.

Create a project with the following details:

  • ProjectName: LifeCycle
  • PackageName: sat.tuts4mobile.lifecycle
  • ActivityName: LifeCycleActivity

In the file, add the following code:

package sat.tuts4mobile.lifecycle; 
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class LifeCycleActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        Toast.makeText(LifeCycleActivity.this,"ON CREATE", Toast.LENGTH_SHORT).show(); 
    protected void onStart() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON START", Toast.LENGTH_SHORT).show();
    protected void onResume() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON RESUME", Toast.LENGTH_SHORT).show(); 
    protected void onPause() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON PAUSE", Toast.LENGTH_SHORT).show(); 
    protected void onRestart() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON RESTART", Toast.LENGTH_SHORT).show(); 
    protected void onStop() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON STOP", Toast.LENGTH_SHORT).show(); 
    protected void onDestroy() {
       // TODO Auto-generated method stub
        Toast.makeText(LifeCycleActivity.this,"ON DESTROY", Toast.LENGTH_SHORT).show();
  1. When you run the following code, you see when the application starts there are three toast messages that come after one another. First is ON CREATE , second is ON START and third is ON RESUME.
  2. When you press the home button from the emulator, ON PAUSE and ON STOP methods call.
  3. And when you open the application again ON RESTART, ON START and ON RESUME methods call.
  4. And at last, when you press the back button from emulator, ON PAUSE , ON STOP and ON DESTROY method calls.

Points of Interest

You can also follow my blog at for mobile application related articles.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Satyam A.
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
ThatsAlok4-Sep-13 16:55
memberThatsAlok4-Sep-13 16:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 Sep 2013
Article Copyright 2013 by Satyam A.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid