2022年android中手机切屏是activity生命周期的变化[收 .pdf
《2022年android中手机切屏是activity生命周期的变化[收 .pdf》由会员分享,可在线阅读,更多相关《2022年android中手机切屏是activity生命周期的变化[收 .pdf(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、android 点滴(12) - 1. Android横竖屏切换时, Activity的生命周期的变化(面试题 ) 2011/10/15 10:49:48 | 阅读 40 次1.Android 横竖屏切换时,Activity的生命周期的变化。Activity的生命周期/Files/cody1988/ActivityLifeCycle.rar完整生命周期 (the entire lifetime) onCreate , onDestroy 在创建和销毁的时候调用。在 onCreate 中初始化全局 资源 ,在onDestroy 中销毁资源。名师资料总结 - - -精品资料欢迎下载 - - - -
2、 - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 23 页 - - - - - - - - - 可见生命周期 (the visible lifetime) onStart , onStop 这是 Activity可见,但是未必可交互即未必在最前面。维护着用户 可见的资源。前景周期 (the for eground lifetime) onResume , onPause 此时 Activity在最前面,可与用户交互 。一个 Activity可在 Resume与 Pause 之间频繁的切换例如设备休眠。因此这两个方法中只有相当轻量
3、级的调用。横竖屏切换时Activity的生命周期的变化与activity的 configChanges的配置有关。1.configChanges不配置运行:初始时 TextView 显示的 内容 为“Hello World, LifeCycle!”,点击Button 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 23 页 - - - - - - - - - 竖屏切换为横屏:此时 TextView 的内容重新变为“Hello World, LifeCycle!”横屏切换为
4、竖屏:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 23 页 - - - - - - - - - 2.配置 configChanges 运行,点击Button 竖屏切换为横屏:没有发生变化横屏切换为竖屏:没有发生变化结论:横竖屏切换时Activity的生命周期与configChanges的配置相关。 1. 如果不配置,则要先销毁Activity再创建,销毁的过程 中会调用 onSaveInstanceState,2. 如果配置configChanges为 Orienta
5、tion则不销毁名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 23 页 - - - - - - - - - 横竖屏切换时候activity的生命周期博客分类:AndroidAndroidXML通过以下部分代码 , 我们可以了解清楚Activity页面在横 , 竖屏切换时 , 生命周期的变化 : Java 代码1.public class AndroidLifecycle extends Activity 2.3. public void onCreate(Bundle
6、savedInstanceState) 4. System.out.println(First Activity =onCreate()=); 5. super.onCreate(savedInstanceState); 6. setContentView(R.layout.main); 7. 8.9.Override10. protected void onSaveInstanceState(Bundle outState) 11. System.out 12. .println(First Activity =onSaveInstanceState()=); 13. super.onSav
7、eInstanceState(outState); 14. 15.16.Override17. protected void onRestoreInstanceState(Bundle outState) 18. System.out 19. .println(First Activity =onRestoreInstanceState()=); 20. super.onRestoreInstanceState(outState); 21. 22.23.Override24. public void onConfigurationChanged(Configuration newConfig)
8、 25. System.out 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 23 页 - - - - - - - - - 26. .println(First Activity =onConfigurationChanged()=); 27. super.onConfigurationChanged(newConfig); 28. 29.30. / Called after onCreate or after onRestart when the activity h
9、ad been 31. / stopped, but is now again being displayed to the user. It will be 32. / followed by onResume 33. protected void onStart() 34. System.out.println(First Activity =onStart()=); 35. super.onStart(); 36. 37.38. / Called after onRestoreInstanceState, onRestart, or onPause, for your 39. / act
10、ivity to start interacting with the user 40. protected void onResume() 41. System.out.println(First Activity =onResume()=); 42. super.onResume(); 43. 44.45. / Called as part of the activity lifecycle when an activity is going into 46. / the background, but has not (yet) been killed 47. protected voi
11、d onPause() 48. System.out.println(First Activity =onPause()=); 49. super.onPause(); 50. 51.52. / Called when you are no longer visible to the user. You will next receive 53. / either onRestart, onDestroy, or nothing, depending on later user 54. / activity. 55. protected void onStop() 56. System.out
12、.println(First Activity =onStop()=); 57. super.onStop(); 58. 59.60. / Perform any final cleanup before an activity is destroyed 61. protected void onDestroy() 62. System.out.println(First Activity =onDestroy()=); 63. super.onDestroy(); 64. 65.66. / Called after onStop when the current activity is be
13、ing re-displayed to 67. / the user (the user has navigated back to it). It will be followed by 68. / onStart and then onResume 69. protected void onRestart() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 23 页 - - - - - - - - - 70. System.out.println(First Acti
14、vity =onRestart()=); 71. super.onRestart(); 72. 73. Xml 代码1.2.4. 5.6.8.9.10.11.12.13.14. 1、新建一个 Activity,并把各个生命周期打印出来2、运行 Activity,得到如下信息10-23 02:35:54.261: INFO/chenys(4385): onCreate- 10-23 02:35:54.271: INFO/chenys(4385): onStart- 10-23 02:35:54.286: INFO/chenys(4385): onResume- 3、按 crtl+f12切换成横屏
15、时10-23 02:36:58.331: INFO/chenys(4385): onSaveInstanceState- 10-23 02:36:58.411: INFO/chenys(4385): onPause- 10-23 02:36:58.462: INFO/chenys(4385): onStop- 10-23 02:36:58.481: INFO/chenys(4385): onDestroy- 10-23 02:36:58.572: INFO/chenys(4385): onCreate- 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -
16、- - - - - - 名师精心整理 - - - - - - - 第 7 页,共 23 页 - - - - - - - - - 10-23 02:36:58.622: INFO/chenys(4385): onStart- 10-23 02:36:58.632: INFO/chenys(4385): onRestoreInstanceState- 10-23 02:36:58.642: INFO/chenys(4385): onResume- 4、再按 crtl+f12切换成竖屏时,发现打印了两次相同的log10-23 02:38:14.172: INFO/chenys(4385): onSa
17、veInstanceState- 10-23 02:38:14.172: INFO/chenys(4385): onPause- 10-23 02:38:14.172: INFO/chenys(4385): onStop- 10-23 02:38:14.172: INFO/chenys(4385): onDestroy- 10-23 02:38:14.281: INFO/chenys(4385): onCreate- 10-23 02:38:14.301: INFO/chenys(4385): onStart- 10-23 02:38:14.312: INFO/chenys(4385): on
18、RestoreInstanceState- 10-23 02:38:14.331: INFO/chenys(4385): onResume- 10-23 02:38:14.812: INFO/chenys(4385): onSaveInstanceState- 10-23 02:38:14.852: INFO/chenys(4385): onPause- 10-23 02:38:14.861: INFO/chenys(4385): onStop- 10-23 02:38:14.892: INFO/chenys(4385): onDestroy- 10-23 02:38:14.921: INFO
19、/chenys(4385): onCreate- 10-23 02:38:15.021: INFO/chenys(4385): onStart- 10-23 02:38:15.031: INFO/chenys(4385): onRestoreInstanceState- 10-23 02:38:15.111: INFO/chenys(4385): onResume- 5、修改 AndroidManifest.xml,把该 Activity添加android:configChanges=orientation,执行步骤 310-23 02:42:32.201: INFO/chenys(4875)
20、: onSaveInstanceState- 10-23 02:42:32.232: INFO/chenys(4875): onPause- 10-23 02:42:32.301: INFO/chenys(4875): onStop- 10-23 02:42:32.311: INFO/chenys(4875): onDestroy- 10-23 02:42:32.402: INFO/chenys(4875): onCreate- 10-23 02:42:32.471: INFO/chenys(4875): onStart- 10-23 02:42:32.471: INFO/chenys(487
21、5): onRestoreInstanceState- 10-23 02:42:32.481: INFO/chenys(4875): onResume- 6、再执行步骤 4,发现不会再打印相同信息, 但多打印了一行onConfigChanged10-23 02:44:41.151: INFO/chenys(4875): onSaveInstanceState- 10-23 02:44:41.151: INFO/chenys(4875): onPause- 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - -
22、- - - - 第 8 页,共 23 页 - - - - - - - - - 10-23 02:44:41.151: INFO/chenys(4875): onStop- 10-23 02:44:41.151: INFO/chenys(4875): onDestroy- 10-23 02:44:41.371: INFO/chenys(4875): onCreate- 10-23 02:44:41.421: INFO/chenys(4875): onStart- 10-23 02:44:41.521: INFO/chenys(4875): onRestoreInstanceState- 10-2
23、3 02:44:41.541: INFO/chenys(4875): onResume- 10-23 02:44:42.002: INFO/chenys(4875): onConfigurationChanged-1 7、把步骤 5 的 android:configChanges=orientation 改成android:configChanges=orientation|keyboardHidden,执行步骤 3,就只打印 onConfigChanged10-23 02:46:43.762: INFO/chenys(5193): onConfigurationChanged-2 8、执行步
24、骤 410-23 02:47:27.652: INFO/chenys(5193): onConfigurationChanged-2 10-23 02:47:27.902: INFO/chenys(5193): onConfigurationChanged-1 总结:1、不设置 Activity的 android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次2、设置 Activity的 android:configChanges=orientation时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次3、设置 Activity的an
25、droid:configChanges=orientation|keyboardHidden时, 切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 23 页 - - - - - - - - - Android 面试锦集1. Intent的几种有关 Activity启动的方式有哪些,你了解每个含义吗? 这里 Android123 提示大家, Intent的一些标记有FLAG_ACTIVITY
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年android中手机切屏是activity生命周期的变化收 2022 android 手机 切屏是 activity 生命周期 变化
限制150内