Lifecycle aware component

Keywords: Mobile Fragment Google Android less


brief introduction

Lifecycle is one of Google's recommended architecture components

Lifecycle aware components can perform actions in response to changes in the lifecycle state of another component, such as Activity and Fragment. These components help you write more organized and often streamlined code that is easier to maintain.

By using lifecycle aware components, you can move component dependent code from lifecycle methods into the component itself.

Let's describe a business scenario to help us better understand the meaning of the above sentence.

When we use the custom player component in our Activity, we usually need to do some play and pause operations in the onResume and onPause of the Activity, and stop / release the player in onDestroy. When there are more similar components in our Activity, the logic of life cycle methods such as onResume and onPause will be too complex and difficult to maintain. In this scenario, by using Lifecycle components, the original code logic in the Activity can be integrated into the player and other components, making the logic of the components more introverted and easier to maintain.


Lifecycle enables other components to perceive the life cycle changes of another component (Activity, Fragment, etc.) through observer mode.


In the interface, only one method getLifecycle() is defined to return a Lifecycle object, which can let other objects have a Lifecycle to implement the Lifecycle function.

After Android Support Library 26.1.0, activity and fragment have implemented lifecycle owner by default.


The code is as follows:

public class MyObserver implements LifecycleObserver {
    public void connectListener() {
        //TOOD: ...

    public void disconnectListener() {
        //TODO: ...

//In Activity or Fragment
myLifecycleOwner.getLifecycle().addObserver(new MyObserver());

In this way, when the life cycle of the Activity changes, MyObserver can listen and do the corresponding processing.

Custom View:

public class TestLifeCycleView extends TextView {
    private static final String TAG = TestLifeCycleView.class.getSimpleName();

    private LifecycleObserver lifeCycleObserver;

    public TestLifeCycleView(Context context) {
        this(context, null);

    public TestLifeCycleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    public TestLifeCycleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    public LifecycleObserver getLifeCycleObserver(){
        if(lifeCycleObserver == null){
            lifeCycleObserver = new LifecycleObserver() {
                public void onLifeOnCreate(){
                    Log.d(TAG, "onLifeOnCreate");

                public void onLifeOnResume(){
                    Log.d(TAG, "onLifeOnResume");

                public void onLifePause(){
                    Log.d(TAG, "onLifePause");

                public void onLifeDestroy(){
                    Log.d(TAG, "onLifeDestroy");

        return lifeCycleObserver;

testLifeCycleView = findViewById(;


By using the observer mode, Lifecycle solves the problem that the life cycle methods of Activity and Fragment are too bloated and difficult to maintain. It also makes the relevant logic operations of user-defined View introverted into the class, making the code more concise, less coupled with Activity, and easier to maintain.

Posted by killerofet on Wed, 13 May 2020 03:20:25 -0700