Jetpack为我们提供了两个类,LifecyleOwner(被观察者)和LifecyleObserver(观察者),通过观察者模式实现对页面生命周期的监听。
class LifeCycleListener(val activity: Activity, private val onChangeListener: OnChangeListener) :LifecycleObserver {
companion object{
init {
Log.e("tagLifeCycleListener", ": 执行" )
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreated(){
onChangeListener.onChange("回调 onCreated 执行")
Log.e("tagLifeCycleListener", ":onCreated 执行" )
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private fun onResume(){
Log.e("tagLifeCycleListener", ":onResume 执行" )
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private fun onPause(){
Log.e("tagLifeCycleListener", ":onPause 执行" )
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e("tagLifeCycleListener", ":onDestroy 执行" )
}
interface OnChangeListener{
fun onChange(des:String)
}
}
Log.e(TAG, "onCreate: 执行", )
lifecycleListener = LifeCycleListener(this, object : LifeCycleListener.OnChangeListener {
override fun onChange(des: String) {
Log.e(TAG, "接受到 onChange: ")
}
})
//lifecycle将观察者和被观察者绑定,解决组件对activity生命周期的依赖问题
lifecycle.addObserver(lifecycleListener)
override fun onResume() {
super.onResume()
Log.e(TAG, "onResume: 执行", )
}
override fun onPause() {
super.onPause()
Log.e(TAG, "onPause: 执行", )
}
override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy: 执行", )
}
class MyServiceObserver :LifecycleObserver{
private val MyServiceObserverTAG = "MyServiceObserver"
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun onStart(){
Log.e(MyServiceObserverTAG, "start: 执行" )
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e(MyServiceObserverTAG, "onDestroy: 执行" )
}
}
class MyService: LifecycleService() {
private var myServiceObserver:MyServiceObserver = MyServiceObserver()
init {
lifecycle.addObserver(myServiceObserver)
}
}
fun launchService(view: View) {
startService(Intent(this,MyService::class.java))
}
fun closeService(view: View) {
stopService(Intent(this,MyService::class.java))
}
class ApplicationObserver:LifecycleObserver {
private val ApplicationObserverTAG = "ApplicationObserver"
/**
* 监听应用程序的onCreate方法,整个生命周期只会调用一次
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreated(){
Log.e(ApplicationObserverTAG, "Application onCreated: 执行" )
}
/**
* 监听应用程序的处于前台时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun onStart(){
Log.e(ApplicationObserverTAG, "Application onStart: 前台执行" )
}
/**
* 监听应用程序的处于前台时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private fun onResume(){
Log.e(ApplicationObserverTAG, "Application onResume: 前台执行" )
}
/**
* 监听应用程序的处于后台时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private fun onPause(){
Log.e(ApplicationObserverTAG, "Application onPause: 后台执行" )
}
/**
* 监听应用程序的处于后台时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private fun onStop(){
Log.e(ApplicationObserverTAG, "Application onStop: 后台执行" )
}
/**
* 永远不会调用,系统不会分发调用on_destroy事件
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e(ApplicationObserverTAG, "Application onDestroy: 不会执行" )
}
}
class APP: Application() {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationObserver())
}
}