Android Activity

In Android studio Activity is a starting point OR entry point to start an application. Activity performs several tasks when we start an android application with the call back methods. we start an application first it call’s a Call back method i.e. onCreate() method and ends the application when we close then it call’s onDestroy() call back method. Activity has default  life cycle methods based on that it android application runs OR performs it’s tasks.

we will go into more details about Activity Life Cycle. Follow the activity lifecycle diagram given below.

 

 

  1. onCreate():

When we start an Application OR when we launch Activity it call’s onCreate() call back method to start an application and then it call’s  onStart() call  back method to perform task whatever we select that task will be performed by executing onStart() method as a starting  point after onCreate() method.

2.onStart():

When onStart() method executed the application will be visible to the user’s.

  1. onResume():

When the user wants to perform some other activity from the current activity then user navigates to the new activity.ie. example when user watching videos then he want to receive a incoming call then he receive a call then the first activity goes to resume state by calling onResume() call back method.

4.onPause():

When user pause the application OR activity then it call’s onPause() call back method and goes to pause state or pause mode until user start it again. In android to perform two activities when one activity resumes another will be paused and when we resume the paused activity the resumed activity goes to pause .

5.onStop():

When user takes one step back from the current activity then it calls onStop() call back method. The activity no longer will appear. When user starts a new activity that covers entire screen then also it call’s onStop() call back method to stop previous activity and terminates  the activity.

6.onDestroy():

When user closes the application OR activity Then it call’s onDestroy() call back method  to close or shutdown the activity.

7.  onRestart():

If user want to restart the activity after stopping it then onRestart() call back method will be called then the activity starts again and performs the task of that specified activity.

Bwlow Example Describes More about the Android LifeCycle Methods:

 

AndroidManifest.xml:

This AndrodManifest File has package name, name of the activity i.e. MainActivity and AppTheme etc.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shyamkumar.activitylifecycle">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java :

The java class has OnCreate() as a starting point of the Activity. Inside onCreate() we are calling setContentView() method it takes activity_main.xml file as input. It executes this method and loads activity_main.xml layout file inside java class.

When onStart() call back method is executed then this activity_main.xml file is executes and displays the our OR Result or View to the user.

package com.example.shyamkumar.activitylifecycle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    String msg = "Activity Life Cycle : ";
// the first LifeCycle Method of Android id onCreate() Inside this method it call setContentView method it takes
   // activity_main.xml file as input and loads that file

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //  when the activity is going to  visible then this method executes and displays the result.
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(msg, "The onStart() LifeCycle Method");
    }

   // when the activity has become visible mode stays in that mode for long it executes this method
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(msg, "The onResume() LifeCycle Method");
    }

    //  when another activity is taking focus or when we minimise then this method executes
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(msg, "The onPause() LifeCycle Method");
    }

    //  when the activity is closed then this method executes and activity longer visible.
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(msg, "The onStop() LifeCycle Method");
    }

    // After executing onstop() method it executes ondestroy method to destroy the activity means closed the application here
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(msg, "The onDestroy() LifeCycle Method ");
    }
}

activity_main.xml :

09-27 23:36:28.390 8466-8466/com.example.shyamkumar.activitylifecycle /Activity Life Cycle :: The onStart() LifeCycle Method

When onStart() LifeCycle method executes then it executes activity_main.xml layout file that is a view and executes

and displays theresulti.e. “Hellow world!”

09-27 23:36:28.403 8466-8466/com.example.shyamkumar.activitylifecycle /Activity Life Cycle :: The onResume() LifeCycle Method

When the activity in the same mode for a time then it executes onResume() LifeCycle method as shown in above Log files

When we close the application or when we take back one step in emulator or Mobile then it executes onPause() Lifecycle Method

09-27 23:37:29.836 8466-8466/com.example.shyamkumar.activitylifecycle /Activity Life Cycle :: The onPause() LifeCycle Method

After executing OnPause() LifeCycle Method it Executes onStop() Lifecycle Method to stop the Activity as shown in below Log File.

09-27 23:37:31.115 8466-8466/com.example.shyamkumar.activitylifecycle /Activity Life Cycle :: The onStop() LifeCycle Method

After executing onStop() LifeCycle Method it executes onDestroy() LifeCycle method to destroy the Activity Completely. As shown in below LOG files

09-27 23:37:31.118 8466-8466/com.example.shyamkumar.activitylifecycle /Activity Life Cycle :: The onDestroy() LifeCycle Method

activity_main.xml :

This is the view of the Activity on will be loaded inside onCreate() method of MainActivity. When onStart() LifeCycle method executes then this layout file executes and displays the view to the user. This layout file used a ConstraintLayout and TextView to display “Hello World!”

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

 

 

Congratulations You Have Learned Android Activity LifeCycle Methods with Example

Leave a Reply

Categories