Android activity life-cycle tutorial

We will learn about the android activity life-cycle.Before developing the android application you must have knowledge about android activity life-cycle that how it works.

An activity represents the single screen in which user interact.It is like window or frame.

With the help of Activity,we can place all the components on the sinle screen and perform the different types of action.

# Graphical Image of Android activity life-cycle:

Android activity life-cycle

#Android Activity Life-Cycle method is given below:

MethodDescription
onCreate( )When the activity first time created
onStart( )When the activity becomes visible to the user
onResume( )When the user start interact with the application
onPause( )When the activity not visible to the user
onStop( )When the activity no longer visible to the user
onDestroy( ) It is called before the activity is destroyed by the system
onRestart( ) It is called when the activity start after the stopping

# Now we will learn Android activity life-cycle with the help of Example:

1.Creating layout ie XML

In Android Studio,go to res/layout/lifecycle.xml


lifecycle .xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"/>  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text=" "/> 
    
</android.support.constraint.ConstraintLayout>

# Android activity life-cycle example:

Here we will see the complete life-cycle of android.Here we will see the content using the Logcat.

MainActivity.class

import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log; 
public class  MainActivity extends Activity
{
 @Override  
 protected void onCreate(Bundle savedInstanceState) 
{  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.lifecycle);  
 Log.d("one","oncreatelayout");  
 }  

@Override
protected void onStart()
{
super.onStart();
Log.d("two","onstartlayout");
}

@Override
protected void onResume()
{
super.onResume();
Log.d("three","onresumelayout");
}

@Override
protected void onPause()
{
super.onPause();
Log.d("four","onpauselayout");
}

@Override
protected void onStop()
{
super.onStop();
Log.d("five","onstoplayout");
}

@Override
protected void onDestroy()
{
super.onDestroy();
Log.d("six","ondestroylayout");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.d("seven","onrestartlayout");
}
}

Output:

When we will run the program on the emulator or other device then output can see on the “Logcat”.

one oncreatelayout

two onstartlayout

three onresumelayout

four onpauselayout

five onstoplayout

six ondestroylayout

seven onrestartlayout

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

2 thoughts on “Android activity life-cycle tutorial

Leave a Reply

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