Architecture Components

Lifecycle-Aware Components | Architecture Components

Lifecycle-Aware Components perform actions in response to a change in the lifecycle status of LifeCycleOwner, such as activities and fragments. These components help you produce better organized, and lighter-weight code, that is easier to maintain.

Lifecycle-aware component is deal with three main classes-

LifeCycle

A LifeCycle object is an instance that can retrieve information about the current lifecycle of a LifeCycleOwner that’s is Fragment or Activity, The LifeCycle instance provides information about the lifecycle of the owner in the terms of Events and States.

Events

Basically, The Events are dispatched with respect to the LifeCycleOwner events such as onCreate(), onStart(), onStop(),onResume() … check in below figure.

Source Medium
State

For onCreate() event state is CREATED, onStart() is STARTED, for onPause() it is PAUSED and onStop() is STOPPED and similarly for onDestroy event DESTROYED, so each event of lifecycler there is state.

Source Medium

Suppose we have one activity and this activity has a lifecycle of its own, hence this is a LifeCycleOwner. That means any activity or fragment that has lifecycle will be a LifeCycleOwner. In architecture components, we have LifeCycle as the class for the LifeCycleOwner and this class uses the enumeration of event and State to specify the state of LifeCycle of the LifeCycleOwner.

It tracks the lifecycle status for the associated component using this enumeration. so for each event of the LifeCycle there is a state.

For onCreate() event state is CREATED, onStart() is STARTED, for onPause() it is PAUSED and onStop() is STOPPED and similarly for onDestroy event DESTROYED, so each event of lifecycler there is state.  

LifeCycleOwner

In API 26 and onward the Fragments and Activities implements the LifeCycleOwner interface. It has Android LifeCycle so is known as Owner of the Life Cycle Owner.
In other words, any class that implements the LifeCycleOwner interface that is LifeCyclerOwner.

LifeCycleObserver

This LifeCycle class for the LifeCycle of components we have LifeCycle Observer. This LifeCycle of observer is associated with the lifecycle owner for observes the lifecycle of the activity.

Let’s understand with demo app

Move to Android Studio and create a new project with Empty Activity template. I hope you have a clear concept of activity lifecycle and if you don’t please make sure you do. Here is references article.

Open app/build.gradle and include the dependencies for the life cycle of a component.
 def lifecycle_version = "1.1.1"
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
Now we need to create an observer class which will observe the lifecycle of this LifeCycleOwner and perform the actions accordingly. For instance, create a new class and implements LifecycleObserver.
package com.wave.lifecycleawaredemo;


import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.util.Log;

/**
 * Created on : Mar 14, 2019
 * Author     : AndroidWave
 */
public class MainActivityObserver implements LifecycleObserver {
    private static final String TAG = "MainActivityObserver";

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreateEvent() {
        Log.i(TAG, "Observer ON_CREATE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onStartEvent() {
        Log.i(TAG, "Observer ON_START");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResumeEvent() {
        Log.i(TAG, "Observer ON_RESUME");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPauseEvent() {
        Log.i(TAG, "Observer ON_PAUSE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onStopEvent() {
        Log.i(TAG, "Observer ON_STOP");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroyEvent() {
        Log.i(TAG, "Observer ON_DESTROY");
    }

}

Here I have created this LifeCycle of Java class for which the name is MainActivityObserver and This class will implement LifeCycleObserver. The LifeCycleObserver using the annotation of on LifeCycle event to keep track of the LifeCycle events of the LifeCycleOwner. such as ON_CREATE, ON_PAUSE ..

Now moving to the MainActivity, I need to attach the observer to this LifeCycleOwner using addObserver();
package com.wave.lifecycleawaredemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "Owner onCreate");
        getLifecycle().addObserver(new MainActivityObserver());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "Owner onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "Owner onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "Owner onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "Owner onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Owner onDestroy");

    }
}

Now run the project and check how the LifeCycleObserve and LifeCycleOwner behave. Let’s applied filter on android LogCat and fetch info only the output

Output when run first time =>

I/MainActivity: Owner onCreate
I/MainActivityObserver: Observer ON_CREATE
I/MainActivity: Owner onStart
I/MainActivityObserver: Observer ON_START
I/MainActivity: Owner onResume
I/MainActivityObserver: Observer ON_RESUME

Now rotated the screen then output be => 

I/MainActivityObserver: Observer ON_PAUSE
I/MainActivity: Owner onPause
I/MainActivityObserver: Observer ON_STOP
I/MainActivity: Owner onStop
I/MainActivityObserver: Observer ON_DESTROY
I/MainActivity: Owner onDestroy
I/MainActivity: Owner onCreate
I/MainActivityObserver: Observer ON_CREATE
I/MainActivity: Owner onStart
I/MainActivityObserver: Observer ON_START
I/MainActivity: Owner onResume
I/MainActivityObserver: Observer ON_RESUME

Conclusion

  • Lifecycle-aware component respond to change in lifecycle status of the other UI component.
  • The Lifecycle class has held the information on the lifecycle of Fragment and Activity.
  • LifeCycle object uses the Event and State enumeration to track lifecycle status
  • LifeCycleOwner provides the status to the lifecycle-aware component
  • LifeCycleObserver the registers the lifecycle status to respond and perform the action.

Download Sample Project- Lifecycle-Aware Components

Leave a Reply

2000