Lifecycle in Android Architecture Components
Last Updated :
03 Jun, 2024
Lifecycle is one of the Android Architecture Components which was released by Google to make it easier for all the Android developers. The Lifecycle is a class/interface which holds the information about the state of an activity/fragment and also it allows other objects to observe this state by keeping track of it. The LifeCycle component is concerned with the Android LifeCycle events of a component such as an Activity or a Fragment, it has three main classes that we’ll deal with:
- Lifecycle
- Lifecycle Owner
- Lifecycle Observer
1. Lifecycle
Lifecycle is a process that tells us about the Events performed on an Activity/Fragment. We have a lifecycle as a class that has two types of enumerations to track the components, State and Event. Event and State are used to determine the lifecycle. Each event has its own state.
Event
| State
|
---|
OnCreate() | Called when the activity is first created. |
OnStart() | Called when the activity becomes visible to the user. |
OnResume() | Called when the activity starts interacting with the user. |
OnPause() | Called when the current activity is being paused and the previous activity is resumed. |
OnStop() | Called when the activity is no longer visible to the user. |
OnDestroy() | Called before the activity is destroyed by the system(either manually or by the system to conserve memory |
OnRestart() | Called when the activity has been stopped and is restarting again. |

Within the Lifecycle Events, you can declare how your activity behaves when the user leaves and re-enters the activity.
Example:
If you're watching a Youtube video, you might pause the video and terminates the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.
2. Lifecycle Owner
Every Activity has a Lifecycle. This Activity will be a Lifecycle Owner(any activity or fragment can be called a lifecycle owner). When an activity is initialized LifecycleOwner is the one that will be constructed initially. Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle. For example, starting from Support Library 26.1.0 Fragments and Activities implement the LifeCycleOwner interface. One can create custom LifeCycleOwner components by implementing the interface and using a LifeCycleRegistry as described here.
3. Lifecycle Observer
Lifecycle Observer, which observes the activity and keeps track of the lifecycle, and performs an action. The action performed by this lifecycle Observer depends on the lifecycle of the lifecycle Owner. Every lifecycle owner has a lifecycle and based on the event or state of the lifecycle of the owner, the lifecycle observer performs the action.
Java
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("lifecycle", "onCreate invoked");
}
@Override protected void onStart()
{
super.onStart();
Log.e("lifecycle", "onStart invoked");
}
@Override protected void onResume()
{
super.onResume();
Log.e("lifecycle", "onResume invoked");
}
@Override protected void onPause()
{
super.onPause();
Log.e("lifecycle", "onPause invoked");
}
@Override protected void onStop()
{
super.onStop();
Log.e("lifecycle", "onStop invoked");
}
@Override protected void onRestart()
{
super.onRestart();
Log.e("lifecycle", "onRestart invoked");
}
@Override protected void onDestroy()
{
super.onDestroy();
Log.e("lifecycle", "onDestroy invoked");
}
}
Output:
You will not see any output on the emulator or device. You need to open logcat window.

Now see on the logcat: OnCreate, OnStart, and OnResume methods are invoked. Now click on the HOME button. You will see on the Pause method is invoked. After a while, you will see onStop method is invoked.
Now see on the emulator. It is on the home. Now click on the center button to launch the app again. Now click on the app icon.
Now see on the logcat: onRestart, onStart and onResume methods are invoked. If you see on the emulator, the application is started again. Now click on the back button. Now you will see onPause methods are invoked. After a while, you will see onStop and Destroy methods are invoked.
Similar Reads
LiveData in Android Architecture Components LiveData is one of the android architecture components. LiveData is an observable data holder class. What is the meaning of observable here the observable means live data can be observed by other components like activity and fragments (Ui Controller). The most important thing about LiveData is it ha
5 min read
Jetpack Architecture Components in Android Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single
10 min read
ViewModel in Android Architecture Components ViewModel is part of the android architecture component. Android architecture components are the components that are used to build robust, clean, and scalable apps. Android architecture components hold some classes to manage UI components and Data persistence. The ViewModel class is designed to stor
5 min read
Overview of Navigation in Android Architecture Components Navigation basically in mobile development refers to the interaction between different screens or pieces of contents within the application. Android Jetpack's architecture Component the Navigation is so powerful, providing a consistent and predictable user experience. The navigation component helps
5 min read
Overview of Navigation in Android Architecture Components Navigation basically in mobile development refers to the interaction between different screens or pieces of contents within the application. Android Jetpack's architecture Component the Navigation is so powerful, providing a consistent and predictable user experience. The navigation component helps
5 min read
Overview of Data Binding in Android Architecture Components This article describes the concept behind the Data Binding and MVVM Design Pattern and its components. To try it, open or find a project with messy and unmanageable code, then attempt to use the MVVM components introduced in this article.In many ways, we haven't seen much changes in the software dev
4 min read