Android Activity Lifecycle

Android Activity Lifecycle is controlled by 7 methods of 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 UI components or widgets in a single screen

The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

Let’s see the 7 lifecycle methods of android activity.

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.
File: activity_main.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=“http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=“http://schemas.android.com/tools”  
  5.     android:layout_width=“match_parent”  
  6.     android:layout_height=“match_parent”  
  7.     tools:context=“example.javatpoint.com.activitylifecycle.MainActivity”>  
  8.   
  9.     <TextView  
  10.         android:layout_width=“wrap_content”  
  11.         android:layout_height=“wrap_content”  
  12.         android:text=“Hello World!”  
  13.         app:layout_constraintBottom_toBottomOf=“parent”  
  14.         app:layout_constraintLeft_toLeftOf=“parent”  
  15.         app:layout_constraintRight_toRightOf=“parent”  
  16.         app:layout_constraintTop_toTopOf=“parent” />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

Android Activity Lifecycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.

File: MainActivity.java
  1. package example.javatpoint.com.activitylifecycle;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         Log.d(“lifecycle”,“onCreate invoked”);  
  14.     }  
  15.     @Override  
  16.     protected void onStart() {  
  17.         super.onStart();  
  18.         Log.d(“lifecycle”,“onStart invoked”);  
  19.     }  
  20.     @Override  
  21.     protected void onResume() {  
  22.         super.onResume();  
  23.         Log.d(“lifecycle”,“onResume invoked”);  
  24.     }  
  25.     @Override  
  26.     protected void onPause() {  
  27.         super.onPause();  
  28.         Log.d(“lifecycle”,“onPause invoked”);  
  29.     }  
  30.     @Override  
  31.     protected void onStop() {  
  32.         super.onStop();  
  33.         Log.d(“lifecycle”,“onStop invoked”);  
  34.     }  
  35.     @Override  
  36.     protected void onRestart() {  
  37.         super.onRestart();  
  38.         Log.d(“lifecycle”,“onRestart invoked”);  
  39.     }  
  40.     @Override  
  41.     protected void onDestroy() {  
  42.         super.onDestroy();  
  43.         Log.d(“lifecycle”,“onDestroy invoked”);  
  44.     }  
  45. }