Jetpack-LifeCycle记录

初次见到这个心仪的男人,她就假装扭脚碰洒了一杯水,男人衬衫湿了大半,然后一眼看到始作俑者的明眸皓齿和俏皮笑容,呆在当场。

感知生命周期。

1. LifeCycle创建

新建一个类实现LifecycleObserver接口,接下来就可以定义方法为所欲为了。

package com.litton.myjetpack.lifecycle

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import timber.log.Timber


/**
 * Created by littonishir on 2020/6/22.
 */
class MyLifecycle(val lifecycle: Lifecycle) : LifecycleObserver {

    /**
     * 将Lifecycle对象传进来,
     * 我们就可以在任何地方主动来获取当前的生命周期状态。
     * Lifecycle.State 是一个枚举类型,共如下五种状态类型。
     */
    fun getCurrentState() {
        when (lifecycle.currentState) {
            Lifecycle.State.INITIALIZED -> {
                Timber.e("INITIALIZED")
            }
            Lifecycle.State.CREATED -> {
                Timber.e("CREATED")
            }
            Lifecycle.State.STARTED -> {
                Timber.e("STARTED")
            }
            Lifecycle.State.RESUMED -> {
                Timber.e("RESUMED")
            }
            Lifecycle.State.DESTROYED -> {
                Timber.e("DESTROYED")
            }
        }

    }

    /**
     * Lifecycle.Event 是一个枚举类型,共七种状态类型
     * 这里展示了六种,Lifecycle.Event.ON_ANY 可以匹配任何生命周期回调。
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun create() {
        Timber.e("ON_CREATE")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun start() {
        Timber.e("ON_START")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun resume() {
        Timber.e("ON_RESUME")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun pause() {
        Timber.e("ON_PAUSE")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun stop() {
        Timber.e("ON_STOP")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun destroy() {
        Timber.e("ON_DESTROY")
    }
}

2. LifeCycle使用

只要添加一行这样的代码lifecycle.addObserver( ),MyLifecycle就可以感知生命周期了。

class MyLifecycleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle_test)
        //界面时长记录
        lifecycle.addObserver(mChronometer)
        //lifecycle示例
        lifecycle.addObserver(MyLifecycle(lifecycle))
    }
}

晓示例

  1. 计时器控件
   package com.litton.myjetpack.lifecycle
   
   import android.content.Context
   import android.os.SystemClock
   import android.util.AttributeSet
   import android.widget.Chronometer
   import androidx.lifecycle.Lifecycle
   import androidx.lifecycle.LifecycleObserver
   import androidx.lifecycle.OnLifecycleEvent
   
   
   /**
    * 计时器控件(带有生命周期感应)
    * 记录界面显示时长,只有控件在前台显示时记录时长。
    * Created by littonishir on 2020/6/22.
    */
   class CharonX(context: Context?, attrs: AttributeSet?) : Chronometer(context, attrs),
       LifecycleObserver {
       private var elapsedTime = 0L
   
       @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
       private fun resume() {
           setBase(SystemClock.elapsedRealtime() - elapsedTime)
           start()
       }
   
       @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
       private fun pause() {
           elapsedTime = SystemClock.elapsedRealtime() - base
           stop()
       }
   }

持续更新中...

你可能感兴趣的:(Jetpack-LifeCycle记录)