Android ProcessLifecycleOwner

Android ProcessLifecycleOwner

  • 针对整个应用程序生命周期的监听,和activity数量没有关系
  • onCreate只会调用一次,onDestory不会调用

实现 DefaultLifecycleObserver

kotlin 复制代码
class ApplicationObserver : DefaultLifecycleObserver {

    companion object{
        const val TAG ="ApplicationObserver"
    }
    
    override fun onCreate(owner: LifecycleOwner) {
        Log.i(TAG,"onCreate")
    }

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

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

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

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

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

在Application中添加

kotlin 复制代码
class MyApplication : Application() {

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

通过日志观察,在应用启动会调用 onCreate 方法,退出后台或者kill进程,不会调用 onDestory 方法

相关推荐
ltlovezh1 天前
ROI 编码学习指南:Android 与 FFmpeg 的真实实现边界
android·ffmpeg·音视频开发
心前阳光1 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
utf8mb4安全女神1 天前
MySQL5.7升级到MySQL8.0并进行数据迁移
android
黄林晴1 天前
Android XR DP4 重磅发布:手机 App 直投眼镜,Compose 原生玩转 3D 内容
android·google io
炼川淬海DB1 天前
数据库开发规范
android·adb·数据库开发
2501_915918411 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
天天爱吃肉82181 天前
豆包 vs DeepSeek API 对比分析报告
android·java·大数据·开发语言·功能测试·嵌入式硬件·汽车
问心无愧05131 天前
ctf show web入门123
android·前端·笔记
想你依然心痛1 天前
手机远程控制电脑教程:安卓iOS远程桌面推荐、免费工具配置与远程办公技巧
android·智能手机·电脑