lifecycle

作者: 闫鹏飞写字的地方 | 来源:发表于2022-06-02 17:55 被阅读0次

官方地址:https://developer.android.google.cn/topic/libraries/architecture/lifecycle#kotlin

B站视频:https://www.bilibili.com/video/BV1fY4y1i7BG?p=2&vd_source=c9e619eb6c2ba53337eccc49eb025732

lifecycle出现之前管理生命周期

private const val TAG = "MyTest"

internal class MyLiftCycle(context: Context) {

    fun resume() {
        Log.i(TAG, "resume")
    }

    fun destroy() {
        Log.i(TAG, "destroy")
    }
}

class MainActivity : AppCompatActivity() {

    private val myLiftCycle by lazy {
        MyLiftCycle(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    public override fun onResume() {
        super.onResume()
        myLiftCycle.resume()
    }

    override fun onDestroy() {
        super.onDestroy()
        myLiftCycle.destroy()
    }

}

运行结果:

2022-06-02 16:53:20.749 30934-30934/com.wy.myapplication22 I/MyTest: resume
2022-06-02 16:53:41.938 30934-30934/com.wy.myapplication22 I/MyTest: destroy

lifecycle出现之后管理生命周期

private const val TAG = "MyTest"

class MyLiftCycleTest : LifecycleObserver{

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun create(){
        Log.i(TAG, "create")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun start(){
        Log.i(TAG, "start")
    }

}
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        lifecycle.addObserver(MyLiftCycleTest())
    }

}

运行结果:

2022-06-02 17:11:38.415 1128-1128/com.wy.myapplication22 I/MyTest: create
2022-06-02 17:11:38.419 1128-1128/com.wy.myapplication22 I/MyTest: start

监听Application生命周期

implementation 'androidx.lifecycle:lifecycle-process:2.4.0'
class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(MyLiftCycleTest())
    }

}

运行结果:

2022-06-02 17:20:58.349 6487-6487/com.wy.myapplication22 I/MyTest: create
2022-06-02 17:20:58.693 6487-6487/com.wy.myapplication22 I/MyTest: start

LifecycleObserver 已废弃 使用DefaultLifecycleObserver方式

class MyDefaultLifecycleObserver : DefaultLifecycleObserver {

    override fun onResume(owner: LifecycleOwner) {
        Log.i(TAG, "onResume")
    }

    override fun onPause(owner: LifecycleOwner) {
        Log.i(TAG, "onPause")
    }

}
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        lifecycle.addObserver(MyDefaultLifecycleObserver())
    }

}

运行结果:

2022-06-02 17:29:34.193 8151-8151/com.wy.myapplication22 I/MyTest: onResume
2022-06-02 17:29:36.980 8151-8151/com.wy.myapplication22 I/MyTest: onPause

相关文章

网友评论

      本文标题:lifecycle

      本文链接:https://www.haomeiwen.com/subject/rlmqmrtx.html