Open In App

Lifecycle in Android Architecture Components

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Lifecycle
  2. Lifecycle Owner
  3. 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.



Last Updated : 23 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads