前言
Android中用得最多的组件是Activity,而它的生命周期也是最基础的知识,从刚接触Android到工作中会频繁依赖这部分知识。可能大多数人能说出页面新建到页面关闭会走的生命周期:onCreate
、onStart
、onResume
、onPause
、onStop
、onDestory
,但是还有其它使用场景不多的生命周期却鲜为人知,比如onWindowFocusChanged
、onSaveInstanceState
、onRestoreInstanceState
、onRetainNonConfigurationInstance
等,这些生命周期用处很大,它们很容易被忽略或遗忘,今天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之后在onStop
和onDetory
之间,Andriod 9.0之前在onPause
和onStop
之间。
onRestoreInstanceState
它通常用来恢复页面上的数据,仅在页面重建时调用,正常跳转是不会调用的。它在onStart
和onResume
之间调用。页面重建的情况比如旋转屏幕、系统资源充足时恢复页面。
onRetainNonConfigurationInstance
这个函数是在配置变化时调用,比如屏幕旋转。它的返回值是Object,配合getLastNonConfigurationInstance()
函数可以获取保存的Object对象。这意味着它可以保存任意类型的数据,而onSaveInstanceState
只能保存bundle
类型的数据。这也是Jetpack
中ViewModel
框架能保存数据的原理。不过这个函数在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
,等新页面的onResume
和onWindowFocusChanged
执行完,才会走前一个页面的onStop
和onSaveInstanceState
。
页面回退
从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
以下是onSaveInstanceState
和onRestoreInstanceState
的日志代码:
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