android中手机切屏是activity生命周期的变化
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
android点滴(12) -- 1. Android横竖屏切换时,Activity的生命周期的变化
(面试题)
2011/10/15 10:49:48 | 阅读40次
1.Android横竖屏切换时,Activity的生命周期的变化。
Activity的生命周期
/Files/cod y1988/ActivityLifeCycle.rar
完整生命周期(the entire lifetime)
onCreate , onDestroy 在创建和销毁的时候调用。在onCreate中初始化全局资源,在onDestroy中销毁资源。
可见生命周期(the visible lifetime)
onStart , onStop 这是Activity可见,但是未必可交互即未必在最前面。维护着用户可见的资源。
前景周期(the for eground lifetime)
onResume , onPause 此时Activity在最前面,可与用户交互。一个Activity
可在Resume与Pause之间频繁的切换例如设备休眠。因此这两个方法中只有相当轻量级的调用。
横竖屏切换时Activity的生命周期的变化与activity的configChanges的配置有关。
1.configChanges不配置
运行:
初始时TextView显示的内容为“Hello World, LifeCycle!”,点击Button
竖屏切换为横屏:
此时TextView的内容重新变为“Hello World, LifeCycle!”
横屏切换为竖屏:
2.配置configChanges
android:configChanges="orientation" android:label="@str in g/app_name"> ……
运行,点击Button
竖屏切换为横屏:
没有发生变化
横屏切换为竖屏:
没有发生变化
结论:横竖屏切换时Activity的生命周期与configChanges的配置相关。1.如果不配置,则要先销毁Activity再创建,销毁的过程中会调用onSaveInstanceState,
2.如果配置configChanges为Orientation则不销毁
横竖屏切换时候activity的生命周期
博客分类:
Android
AndroidXML
通过以下部分代码,我们可以了解清楚Activity页面在横,竖屏切换时,生命周期的变化:
Java代码
1.public class AndroidLifecycle extends Activity {
2.
3. public void onCreate(Bundle savedInstanceState) {
4. System.out.println("First Activity =======onCreate()========");
5. super.onCreate(savedInstanceState);
6. setContentView(yout.main);
7. }
8.
9.@Override
10. protected void onSaveInstanceState(Bundle outState) {
11. System.out
12. .println("First Activity =======onSaveInstanceState()========");
13. super.onSaveInstanceState(outState);
14. }
15.
16.@Override
17. protected void onRestoreInstanceState(Bundle outState) {
18. System.out
19. .println("First Activity =======onRestoreInstanceState()========");
20. super.onRestoreInstanceState(outState);
21. }
22.
23.@Override
24. public void onConfigurationChanged(Configuration newConfig) {
25. System.out
26. .println("First Activity =======onConfigurationChanged()========");
27. super.onConfigurationChanged(newConfig);
28. }
29.
30. // Called after onCreate — or after onRestart when the activity had 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. // activity 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 void 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.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 being 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() {