Actual Combat of Android Architecture Components——Lifecycle

Note: Please refer to the code involved in this article: Project source code

Lifecycle is an architectural component launched by Google, which is used to perceive the life cycle of components and gives non-component objects the ability to perceive the life cycle

Lifecycle components are used to respond to changes in the life cycle state of another component, such as Activity and Fragment, which can help non-life cycle components to be associated with the life cycle of Activity, Fragment or Service, so as to perform corresponding operations in different life cycles. For other instructions, please refer to Google's official website document Lifecycle

Before I wrote an article "Using Kotlin+MVP+AndroidX to Build an Android Project Framework" , which used the MVP design pattern. Before using Lifecycle, you can see that I added a bunch of methods in the Presenter to correspond to the Activity. The life cycle is as follows:

Base Presenter:

interface BasePresenter {

    fun onCreate()

    fun onStart()

    fun onStop()

    fun onDestory()
}

Then implement MainPresenter in concrete:

class MainPresenter(private val mViewBinder: MainViewBinder?) : BasePresenter {

    override fun onCreate() {}

    override fun onStart() {}

    override fun onStop() {}

    override fun onDestory() {}
}

Called in Activity/Fragment:

class MainActivity : BaseActivity(), MainViewBinder {

    private val mMainPresenter = MainPresenter(this)

    override val contentView: Int
        get() = R.layout.activity_main

    override fun initView() {
    }

    override fun initData() {
    }

    override fun onStart() {
        super.onStart()
        mMainPresenter?.onStart()
    }

    override fun onStop() {
        super.onStop()
        mMainPresenter?.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        mMainPresenter?.onDestory()
    }
}

In each life cycle method, it is necessary to write a method association, which is not elegant at all, so Lifecycle appears, after use:

interface BasePresenter : LifecycleObserver {
    //关联onCreate()
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate(owner: LifecycleOwner)

    //关联onStart()
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart(owner: LifecycleOwner)

    //关联onStop()
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop(owner: LifecycleOwner)

    //关联onDestroy()
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(owner: LifecycleOwner)
}
class MainPresenter(private val mViewBinder: MainViewBinder?) : BasePresenter{

    override fun onCreate(owner: LifecycleOwner) {
        LogUtil.i("onCreate()")
    }

    override fun onStart(owner: LifecycleOwner) {
        LogUtil.i("onStart()")
    }

    override fun onStop(owner: LifecycleOwner) {
        LogUtil.i("onStop()")
    }

    override fun onDestroy(owner: LifecycleOwner) {
        LogUtil.i("onDestroy()")
    }
}
class MainActivity : BaseActivity(), MainViewBinder {
    private val mMainPresenter = MainPresenter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //lifecycle(getLifecycle())在AppCompatActivity已经定义了,直接使用
        lifecycle.addObserver(mMainPresenter)//关联起来
    }
}

In this way, Presenter can be associated with the life cycle of Activity.

Let's talk about the steps to use:

1. Introduce dependencies:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

If your project does not use the androidx family bucket, you can also quote the following separately:

implementation "android.arch.lifecycle:common-java8:1.1.0"

2. The associated objects need to implement the LifecycleObserver interface

class MainPresenter() : LifecycleObserver{}

There are two methods:

  • Implement the DefaultLifecycleObserver interface, and then rewrite the lifecycle method inside; androidx does not have this by default, and needs to introduce the common-java8 version
  • Implement the LifecycleObserver interface, and then receive changes in the lifecycle through annotations;

I have written two ways in my project source code, you can check it yourself

3. Add the corresponding @OnLifecycleEvent annotation in the method of the object:

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
    LogUtil.i("onCreate()")
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
    LogUtil.i("onStart()")
}

4. Call in Activity/Fragment:

 lifecycle.addObserver(mMainPresenter)//关联起来

When MainActivity starts, the log:

2019-11-28 10:18:58.293 16238-16238/com.king.frame I/king: onCreate()
2019-11-28 10:18:58.298 16238-16238/com.king.frame I/king: onStart()

When destroying:

2019-11-28 10:19:08.092 16238-16238/com.king.frame I/king: onStop()
2019-11-28 10:19:08.094 16238-16238/com.king.frame I/king: onDestroy()

It's that simple.

Guess you like

Origin blog.csdn.net/gs12software/article/details/103288649