Home » Blog » Activity Lifecycle

Activity Lifecycle

Basically, the one screen of mobile is knows as Activity in Android.

Android Activity Lifecycle is controlled by 7 methods which are included in android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.

An activity is the single screen in android. It is like window or frame of Java.

By the help of activity, you can place all your GUI components or widgets in a single screen.

The activity goes from one state to another which is known as activity lifecycle.

Methods Of Activity Lifecycle

Method Description
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.
onRestart called after your activity is stopped, prior to start.
onDestroy called before the activity is destroyed.

Life Cycle Diagram

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tejsumeru.lifecycle"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tejsumeru.lifecycle.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity_main.xml
<LinearLayout 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:padding="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Title" 
        android:textStyle="bold"/>

</LinearLayout>
MainActivity.xml

Toast displays the pop up menu with short text message. The makeText() method is used with Toast, and at last show() method is used to show pop up.

package com.tejsumeru.lifecycle;

import android.os.Bundle;
import android.app.Activity;
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, "ACTIVITY CREATED", Toast.LENGTH_LONG).show();
      }

      @Override
      protected void onStart() {
      	super.onStart();
      	Toast.makeText(this, "ACTIVITY STARTED", Toast.LENGTH_SHORT).show();
      }
      
      @Override
      protected void onResume() {
      	super.onResume();
      	Toast.makeText(getApplicationContext(), "ACTIVITY RESUMED", Toast.LENGTH_LONG).show();
      }
      
      @Override
      protected void onPause() {
      	super.onPause();
      	Toast.makeText(getApplicationContext(), "ACTIVITY PAUSED", Toast.LENGTH_LONG).show();
      }
      
      @Override
      protected void onStop() {
      	super.onStop();
      	Toast.makeText(getApplicationContext(), "ACTIVITY STOPPED", Toast.LENGTH_LONG).show();
      }
      
      @Override
      protected void onRestart() {
      	super.onRestart();
      	Toast.makeText(getApplicationContext(), "ACTIVITY RESTARTED", Toast.LENGTH_LONG).show();
      }
      
      @Override
      protected void onDestroy() {
      	super.onDestroy();
      	Toast.makeText(getApplicationContext(), "ACTIVITY DESTROYED", Toast.LENGTH_LONG).show();
      }
}

OutPut

Now its Done… Keep It Up

Leave a Reply

Your email address will not be published.