【Android】重温Activity生命周期

前言

Android中用得最多的组件是Activity,而它的生命周期也是最基础的知识,从刚接触Android到工作中会频繁依赖这部分知识。可能大多数人能说出页面新建到页面关闭会走的生命周期:onCreateonStartonResumeonPauseonStoponDestory,但是还有其它使用场景不多的生命周期却鲜为人知,比如onWindowFocusChangedonSaveInstanceStateonRestoreInstanceStateonRetainNonConfigurationInstance等,这些生命周期用处很大,它们很容易被忽略或遗忘,今天Review一下它们的作用以及调用的时机。

基于实时求是的原则,就亲手写一个demo。

笔者原创,转载请注明出处:https://blog.csdn.net/devnn/article/details/137832051

onWindowFocusChanged

它通常用来获取View的宽高,我们知道在onCreate生命周期中直接获取View的宽高会显示0,这是因为View还没有绘制。而在onWindowFocusChanged中能获取宽高,因为View在它之前已经绘制好了。它的调用时机在onResume和performTravesals()函数之后。关于获取View的宽高还有其它几种方法,这里就不展开了。

onSaveInstanceState

它通常用来保存页面的上的数据,用来在页面重建时恢复数据。它的调用时机分两种情况,Andriod 9.0之后在onStoponDetory之间,Andriod 9.0之前在onPauseonStop之间。

onRestoreInstanceState

它通常用来恢复页面上的数据,仅在页面重建时调用,正常跳转是不会调用的。它在onStartonResume之间调用。页面重建的情况比如旋转屏幕、系统资源充足时恢复页面。

onRetainNonConfigurationInstance

这个函数是在配置变化时调用,比如屏幕旋转。它的返回值是Object,配合getLastNonConfigurationInstance() 函数可以获取保存的Object对象。这意味着它可以保存任意类型的数据,而onSaveInstanceState只能保存bundle类型的数据。这也是JetpackViewModel框架能保存数据的原理。不过这个函数在ComponentActivity中被重写成了final修饰,因此如果你的Activity继承AppCompatActivity是无法重写的。

下面观察一下Demo日志,加深一下我们的认识。

启动App

App启动到MainActivity:

shell 复制代码
----------------------------------------启动App----------------------------------------------------
2024-04-16 16:37:23.737 8607-8607/com.devnn.demo I/MyApp: attachBaseContext

2024-04-16 16:37:23.956 8607-8607/com.devnn.demo I/MyApp: onCreate

2024-04-16 16:37:24.984 8607-8607/com.devnn.demo I/MainActivity: onCreate
2024-04-16 16:37:25.516 8607-8607/com.devnn.demo I/MainActivity: onStart
2024-04-16 16:37:25.527 8607-8607/com.devnn.demo I/MainActivity: onResume
2024-04-16 16:37:26.604 8607-8607/com.devnn.demo I/Choreographer: Skipped 62 frames!  The application may be doing too much work on its main thread.
2024-04-16 16:37:27.141 8607-8607/com.devnn.demo I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
2024-04-16 16:37:27.440 8607-8607/com.devnn.demo I/MainActivity: onWindowFocusChanged,hasFocus=true

由于无关日志比较多,删除了一些无关日志。

页面跳转

从MainActivity跳转到DemoActivity1:

shell 复制代码
----------------------------------------跳转-------------------------------------------------------------
2024-04-16 16:40:20.459 8607-8607/com.devnn.demo I/MainActivity: onPause
2024-04-16 16:40:20.872 8607-8607/com.devnn.demo I/DemoActivity1: onCreate
2024-04-16 16:40:20.926 8607-8607/com.devnn.demo I/DemoActivity1: onStart
2024-04-16 16:40:20.928 8607-8607/com.devnn.demo I/DemoActivity1: onResume
2024-04-16 16:40:22.134 8607-8607/com.devnn.demo I/MainActivity: onWindowFocusChanged,hasFocus=false
2024-04-16 16:40:22.195 8607-8607/com.devnn.demo I/Choreographer: Skipped 69 frames!  The application may be doing too much work on its main thread.
2024-04-16 16:40:22.358 8607-8607/com.devnn.demo I/DemoActivity1: onWindowFocusChanged,hasFocus=true
2024-04-16 16:40:22.767 8607-8607/com.devnn.demo I/MainActivity: onStop
2024-04-16 16:40:22.772 8607-8607/com.devnn.demo I/MainActivity: onSaveInstanceState

这里要注意的是,跳转新页面首先会走前一个页面的onPause,等新页面的onResumeonWindowFocusChanged执行完,才会走前一个页面的onStoponSaveInstanceState

页面回退

从DemoActivity1回退到MainActivity:

shell 复制代码
----------------------------------------回退----------------------------------------------------------------
2024-04-16 16:42:03.231 8607-8607/com.devnn.demo I/DemoActivity1: onPause
2024-04-16 16:42:03.267 8607-8607/com.devnn.demo I/MainActivity: onStart
2024-04-16 16:42:03.273 8607-8607/com.devnn.demo I/MainActivity: onResume
2024-04-16 16:42:03.302 8607-8607/com.devnn.demo I/DemoActivity1: onWindowFocusChanged,hasFocus=false
2024-04-16 16:42:03.658 8607-8607/com.devnn.demo I/MainActivity: onWindowFocusChanged,hasFocus=true
2024-04-16 16:42:04.308 8607-8607/com.devnn.demo I/DemoActivity1: onStop
2024-04-16 16:42:04.355 8607-8607/com.devnn.demo I/DemoActivity1: onDestroy

屏幕旋转

shell 复制代码
-------------------------------------------屏幕旋转-----------------------------------------
2024-04-16 16:48:12.004 8607-8607/com.devnn.demo I/MainActivity: onPause
2024-04-16 16:48:12.014 8607-8607/com.devnn.demo I/MainActivity: onStop
2024-04-16 16:48:12.015 8607-8607/com.devnn.demo I/MainActivity: onSaveInstanceState
2024-04-16 16:48:12.016 8607-8607/com.devnn.demo I/MainActivity: onDestroy
2024-04-16 16:48:12.346 8607-8607/com.devnn.demo I/MainActivity: onCreate
2024-04-16 16:48:12.577 8607-8607/com.devnn.demo I/MainActivity: onStart
2024-04-16 16:48:12.579 8607-8607/com.devnn.demo I/MainActivity: onRestoreInstanceState
2024-04-16 16:48:12.579 8607-8607/com.devnn.demo I/MainActivity: onRestoreInstanceState,data=abc
2024-04-16 16:48:12.579 8607-8607/com.devnn.demo I/MainActivity: onResume
2024-04-16 16:48:12.599 8607-8607/com.devnn.demo I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
2024-04-16 16:48:13.955 8607-8607/com.devnn.demo I/Choreographer: Skipped 80 frames!  The application may be doing too much work on its main thread.
2024-04-16 16:48:13.962 8607-8607/com.devnn.demo I/MainActivity: onWindowFocusChanged,hasFocus=true

以下是onSaveInstanceStateonRestoreInstanceState的日志代码:

kotlin 复制代码
  override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.i("MainActivity", "onSaveInstanceState")
        outState.putString("test", "abc")
    }
  override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i("MainActivity", "onRestoreInstanceState")
        val data = savedInstanceState.getString("test", "nothing")
        Log.i("MainActivity", "onRestoreInstanceState,data=${data}")
    }

彩蛋

细心的读者,可能发现怎么有那么多掉帧日志,这个Demo里其实没有阻塞主线程的代码。这其实跟这篇文章主题无关,保留这个日志也是为了验证一个结论,请读者细品~

笔者原创,转载请注明出处:https://blog.csdn.net/devnn/article/details/137832051

相关推荐
姑苏风11 分钟前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k3 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小104 小时前
JavaWeb项目-----博客系统
android
风和先行5 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.5 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰6 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶6 小时前
Android——网络请求
android
干一行,爱一行6 小时前
android camera data -> surface 显示
android
断墨先生7 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
无极程序员8 小时前
PHP常量
android·ide·android studio