Android Activity Lifecycle

In this tutorial Android Activity Life Cycle  methods are shown.

There are mainly 4 states of Activity, they are as below:

  • Running: Activity is visible and interacts with the user
  • Paused: Activity is still visible but partially obscured, instance is running but might be killed by the system
  • Stopped: Activity is not visible, instance is running but might be killed by  the system
  • Killed: Activity has been terminated by the system or by a call to its finish() method

There are 7 methods of Activity Life Cycle they are as below:

  1. onCreate(): It is called once when your activity is created
  2. onStart(): It is called after onCreate() when your activity is started
  3. onPause(): It is called when you minimise your current  activity and open other activity
  4. onResume(): It is called whenever you resume your activity which was paused
  5. onStop(): It is called whenever you close your activity and whenever your activity is no longer visible
  6. onRestart(): It is called whenever you restart the application
  7. onDestroy(): It is called once when you destroy or kill your application

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”60dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_below=”@+id/textView1″
android:layout_marginTop=”21dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:layout_below=”@+id/textView2″
android:layout_marginTop=”26dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView3″
android:layout_below=”@+id/textView3″
android:layout_marginTop=”26dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView5″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView4″
android:layout_below=”@+id/textView4″
android:layout_marginTop=”24dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView6″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView5″
android:layout_below=”@+id/textView5″
android:layout_marginTop=”35dp”
android:text=”TextView” />

<TextView
android:id=”@+id/textView7″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView6″
android:layout_below=”@+id/textView6″
android:layout_marginTop=”32dp”
android:text=”TextView” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);
tv6=(TextView)findViewById(R.id.textView6);
tv7=(TextView)findViewById(R.id.textView7);

tv1.setText(“onCreate invoked”);
Log.d(“Lifecycle”,”onCreate invoked”);
}

@Override
protected void onStart()
{
super.onStart();
tv2.setText(“onStart invoked”);
Log.d(“Lifecycle”,”onStart invoked”);

}

@Override
protected void onResume()
{
super.onResume();
tv3.setText(“onResume invoked”);
Log.d(“Lifecycle”,”onResume invoked”);

}

@Override
protected void onPause()
{
super.onPause();
tv4.setText(“onPause invoked”);
Log.d(“Lifecycle”,”onPause invoked”);

}

@Override
protected void onStop()
{
super.onStop();
tv5.setText(“onStop invoked”);
Log.d(“Lifecycle”,”onStop invoked”);

}

@Override
protected void onRestart()
{
super.onRestart();
tv6.setText(“onRestart invoked”);
Log.d(“Lifecycle”,”onRestart invoked”);

}

@Override
protected void onDestroy()
{
super.onDestroy();
tv7.setText(“onDestroy invoked”);
Log.d(“Lifecycle”,”onDestroy invoked”);

}

@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;
}

}

 

Add a Comment

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