当前位置:   article > 正文

【Android】重温Activity生命周期

【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:

----------------------------------------启动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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

页面跳转

从MainActivity跳转到DemoActivity1:

----------------------------------------跳转-------------------------------------------------------------
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

页面回退

从DemoActivity1回退到MainActivity:

----------------------------------------回退----------------------------------------------------------------
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

屏幕旋转

-------------------------------------------屏幕旋转-----------------------------------------
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

以下是onSaveInstanceStateonRestoreInstanceState的日志代码:

  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}")
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

彩蛋

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/457537
推荐阅读
相关标签
  

闽ICP备14008679号