Lifecycle in Android Architecture Components

23 Mar, 2021
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:
LifecycleLifecycle OwnerLifecycle Observer
Lifecycle
Lifecycle Owner
Lifecycle Observer
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
    
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.
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.
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");    }}
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.
Android-Jetpack
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.