Introduction and use of Android architecture component lifecycle aware

About Lifecycle

Lifecycle architecture component is a life cycle management component, which can separate the life cycle of Activity or Fragment, so as to realize monitoring externally, which can make us release resources in time, avoid memory leakage and program crash.

Why use Lifecycle?

Lifecycle can help us manage the life cycle of Activity or Fragment more friendly. Next, we will compare the way of lifecycle management with that of Presenter.

Presenter management lifecycle

In the common MVP architecture, the Presenter component processes the life cycle of Activity and Fragment, as follows:

// Define Contract

public interface ContentContract {

    // ......

    abstract class Presenter {

        public abstract void onCreate();

        public abstract void onStart();

        public abstract void onResume();

        public abstract void onStop();

        public abstract void onPause();

        public abstract void onDestroy();
    }
}

// Define Presenter

public class ContentPresenter extends ContentContract.Presenter {

    private static final String TAG = ContentPresenter.class.getSimpleName();

    @Override
    public void onCreate() {
        Log.e(TAG, "Presenter ----- onCreate()");
    }

    @Override
    public void onStart() {
        Log.e(TAG, "Presenter ----- onStart()");
    }

    @Override
    public void onResume() {
        Log.e(TAG, "Presenter ----- onResume()");
    }

    @Override
    public void onStop() {
        Log.e(TAG, "Presenter ----- onStop()");
    }

    @Override
    public void onPause() {
        Log.e(TAG, "Presenter ----- onPause()");
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "Presenter ----- onDestroy()");
    }
}

// Used in Activity

public class ContentActivity extends AppCompatActivity {

    private ContentPresenter mPresenter = new ContentPresenter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        mPresenter.onCreate();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }
}

When listening to the life cycle of an Activity or Fragment in a Presenter, you need to process the methods in the Presenter separately, but using the Lifecycle component will be much easier.

Lifecycle observer management lifecycle

You need to implement the lifecycle observer interface and add event annotations.

// Define Lifecycle

public class MainLifecycleListener implements LifecycleObserver {

    private static final String TAG = MainLifecycleListener.class.getSimpleName();

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        Log.e(TAG, "LifecycleObserver ----- onCreate()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        Log.e(TAG, "LifecycleObserver ----- onStart()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        Log.e(TAG, "LifecycleObserver ----- onResume()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
        Log.e(TAG, "LifecycleObserver ----- onStop()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        Log.e(TAG, "LifecycleObserver ----- onPause()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        Log.e(TAG, "LifecycleObserver ----- onDestroy()");
    }

    /**
     * Triggered every time the lifecycle changes
     */
//    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
//    public void onAny(){
//        Log.e(TAG, "LifecycleObserver ----- onAny()");
//    }
}
Initialize LifecycleObserver
public class MainActivity extends AppCompatActivity {

    private MainLifecycleListener mListener = new MainLifecycleListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Add observer
        getLifecycle().addObserver(mListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Remove observer
        getLifecycle().removeObserver(mListener);
    }
}

View case source code

Keywords: Fragment

Added by adguru on Wed, 01 Apr 2020 20:31:51 +0300