Android Activity Lifecycle

android activityAndroid 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.
MethodDescription
onCreatecalled when activity is first created.
onStartcalled when activity is becoming visible to the user.
onResumecalled when activity will start interacting with the user.
onPausecalled when activity is not visible to the user.
onStopcalled when activity is no longer visible to the user.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed.
android activity lifecycle 
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. }  

Output:

You will not see any output on the emulator or device. You need to open logcat.
android activity life cycle example output 1Now see on the logcat: onCreate, onStart and onResume methods are invoked.
android activity life cycle example output 2Now click on the HOME Button. You will see onPause method is invoked.
android activity life cycle example output 3After a while, you will see onStop method is invoked.
android activity life cycle example output 4Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
android activity life cycle example output 5Now click on the lifecycleactivity icon.
android activity life cycle example output 6Now see on the logcat: onRestart, onStart and onResume methods are invoked.
android activity life cycle example output 7If you see the emulator, application is started again.
android activity life cycle example output 8Now click on the back button. Now you will see onPause methods is invoked.
android activity life cycle example output 9After a while, you will see onStop and onDestroy methods are invoked.
android activity life cycle example output 10

Comments

Popular posts from this blog

Display All Contacts from Contacts application in ListView

Most important topics: