Activity Lifecycle

Android Activity Lifecycle

Life Cycle Methods and Callbacks In general, activity lifecycle has seven callback methods:
    1: onCreate()
    2: onStart()
    3: onResume()
    4: onPause()
    5: onStop()
    6: onRestart()
    7: onDestroy()
Whenever we open an Android application, then you see some UI drawn over our screen. That screen is called an Activity. It is the basic component of Android and whenever you are opening an application, then we are opening some activity.
Now let's get into the details of Android Activity Life cycle methods and callbacks.
Take a look at the below figure to understand the life cycle .
Activity Lifecycle
The Activity Lifecycle Methods, Callback and Description -
Method Callback Description
onCreate

onCreate()

This method called when activity is first created.
onStart

onStart()

This method called when activity is becoming visible to the user.
onResume

onResume()

This method called when activity will start interacting with the user.
onPause

onPause()

This method called when activity is not visible to the user.
onStop

onStop()

This method called when activity is no longer visible to the user.
onRestart

onRestart()

This method called after your activity is stopped, prior to start.
onDestroy

onDestroy()

This method called before the activity is destroyed.
So these are the various methods of the Activity Life Cycle.
Now let’s see the situations where the life cycle methods and states will occur.
  • When you open the app it will go through below states:
    onCreate() –> onStart() –> onResume()
  • When you press the back button and exit the app
    onPaused() — > onStop() –> onDestory()
  • When you press the home button or When your phone screen is off
    onPaused() –> onStop()
  • When your phone screen is turned back on or After pressing the home button, again when you open the app from a recent task list
    onRestart() –> onStart() –> onResume()
  • After dismissing the dialog or back button from the dialog or After the call ends
    onResume()
  • If a phone is ringing and user is using the app
    onPause() –> onResume()
Detailed introduction on each of the method is stated as follows:

onCreate()

This is method Called when the activity is first created. This is where you should do all of your normal static set up:
create views, bind data to lists, etc.
This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// Bundle containing previous frozen state
		setContentView(R.layout.activity_main);
		// The content view pointing to the id of layout
		// in the file activity_main.xml
		//Log.d() method has been used to generate log messages −
		Log.d("lifecycle", "The onCreate() Called");

		// you can use below code too just uncomment and comment above code
		//Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
}
				
			
Output:
2021-05-20 10:32:07.672 14199-14199/com.example.Demo E/Lifecycle : The onCreate() Called

onStart()

This method is Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground. It is also called afteronCreate() when the activity is first started.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
    protected void onStart()
    {
        // It will show a message on the screen
        // then onStart is called
     super.onStart();
     Log.d("lifecycle"," The onStart called");
     	// you can use below code
       // Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
    }
}
				
			
Output:
2021-05-20 10:32:07.673 14199-14199/com.example.Demo E/Lifecycle : The onStart() Called

onResume()

This method is Called when the activity start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause() when the activity goes into the background or is closed by the user.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","The onResume called");
    }
}
				
			
Output:
2021-05-20 10:32:07.675 14199-14199/com.example.Demo E/Lifecycle : The onResume() Called

onPause()

This Method is Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","The onPause Called");
    }
}
				
			
Output:
2021-05-20 10:32:07.675 14199-14199/com.example.Demo E/Lifecycle : The onPause() Called

onStop()

This method is Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
Note : This method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
        @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","The onStop Called");
    }
}
				
			
Output:
2021-05-20 10:32:07.678 15199-15199/com.example.Demo E/Lifecycle : The onStop() Called

onRestart()

This method is Called after your activity has been stopped, prior to it being started again. Always followed by onStart() when any activity is revived from background to on-screen.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","The onRestart Called");
    }
}
				
			
Output:
2021-05-20 10:33:03.680 15199-15199/com.example.Demo E/Lifecycle : The onRestart() Called

onDestroy()

The final method call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios check it with the isFinishing() method.

Example :
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","The onDestroy Called");
    }
}
				
			
Output:
2021-05-20 10:33:03.682 15199-15199/com.example.Demo E/Lifecycle : The onDestroy() Called
Demo Android App to Demonstrate Activity Lifecycle in Android In this example, we are displaying the content on the logcat.
				
					import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("lifecycle", "The onCreate() Called");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifecycle","The onStart Called");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","The onResume Called");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","The onPause Called");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","The onStop Called");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","The onRestart Called");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","The onDestroy Called");
    }
}
				
			
Output: You will not see any output on the emulator or device. You need to open logcat window.

Thank You For Visiting Our Website!
And make sure to leave us a comment down below , we will be happy to know if this worked for you.

Happy Coding Mates! 🙂

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shopping Cart