creasylai19

a programmer(https://github.com/creasylai19)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

我的AndroidManifest.xml配置的版本是这样:(因为看其他帖子说不通的版本生命周期不一样

1     <uses-sdk
2             android:minSdkVersion="14"
3             android:targetSdkVersion="19" />
AndroidManifest

第一个Activity的代码:

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.app.AlertDialog;
 5     import android.content.DialogInterface;
 6     import android.content.DialogInterface.OnClickListener;
 7     import android.content.Intent;
 8     import android.content.res.Configuration;
 9     import android.os.Bundle;
10     import android.view.View;
11 
12     public class TestActivityLifeF extends Activity {
13 
14         @Override
15         protected void onCreate(Bundle savedInstanceState) {
16             System.out.println("onCreate");
17             super.onCreate(savedInstanceState);
18             setContentView(R.layout.activity_test_activity_life_f);
19         }
20 
21         public void startTest(View view) {
22             Intent intent = new Intent(TestActivityLifeF.this,
23                     StandardActivity.class);
24             startActivity(intent);
25         }
26 
27         public void startDialog(View view) {
28             new AlertDialog.Builder(TestActivityLifeF.this).setTitle("Test")
29                     .setMessage("just test dialog").setPositiveButton("确定", new OnClickListener() {
30                         
31                         @Override
32                         public void onClick(DialogInterface dialog, int which) {
33                             
34                         }
35                     }).create().show();
36         }
37 
38         @Override
39         protected void onStart() {
40             System.out.println("onStart");
41             super.onStart();
42         }
43 
44         @Override
45         protected void onResume() {
46             System.out.println("onResume");
47             super.onResume();
48         }
49 
50         @Override
51         protected void onPause() {
52             System.out.println("onPause");
53             super.onPause();
54         }
55 
56         @Override
57         protected void onStop() {
58             System.out.println("onStop");
59             super.onStop();
60         }
61 
62         @Override
63         protected void onDestroy() {
64             System.out.println("onDestroy");
65             super.onDestroy();
66         }
67 
68         @Override
69         protected void onRestart() {
70             System.out.println("onRestart");
71             super.onRestart();
72         }
73 
74         @Override
75         protected void onSaveInstanceState(Bundle outState) {
76             System.out.println("onSaveInstanceState");
77             super.onSaveInstanceState(outState);
78         }
79 
80         @Override
81         protected void onRestoreInstanceState(Bundle savedInstanceState) {
82             System.out.println("onRestoreInstanceState");
83             super.onRestoreInstanceState(savedInstanceState);
84         }
85 
86         @Override
87         public void onConfigurationChanged(Configuration newConfig) {
88             System.out.println("onConfigurationChanged");
89             super.onConfigurationChanged(newConfig);
90         }
91     }
TestActivityLifeF

相应布局文件:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2         xmlns:tools="http://schemas.android.com/tools"
 3         android:id="@+id/container"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         tools:context="com.example.androidgov.TestActivityLifeF"
 8         tools:ignore="MergeRootFrame" >
 9 
10         <Button
11             android:layout_width="match_parent"
12             android:layout_height="wrap_content"
13             android:onClick="startTest"
14             android:text="启动Activity" />
15 
16         <Button
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content"
19             android:onClick="startDialog"
20             android:text="启动对话框" />
21 
22     </LinearLayout>
activity_test_activity_life_f

第二个Activity的代码:

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.os.Bundle;
 5 
 6     public class StandardActivity extends Activity {
 7 
 8         @Override
 9         protected void onCreate(Bundle savedInstanceState) {
10             super.onCreate(savedInstanceState);
11             setContentView(R.layout.activity_standard);
12         }
13 
14         
15     }
StandardActivity

布局文件很简单,用默认生成的布局

 

启动第一个Activity:

 

切换到横屏:

看网上的帖子说横屏再切换成竖屏,生命周期的方法会调用两次,但我自己测试只调用了一次,如下(横屏切换回竖屏):

 

启动其他Activity:

从其他Activity中返回:

 

启动对话框和关闭对话框均不调用上面代码中列出的这些方法
最后退出Activity:

最后做个总结:
仅分析Activity中的10个方法:
onCreate()/onStart()/onResume()/onPause()/onStop()/onDestroy()/onRestart()/onSaveInstanceState()/onRestoreInstanceState()/onConfigurationChanged()
1:启动Activity:
onCreate()->onStart()->onResume()
2:竖屏切换横屏:
onPause()->onSaveInstanceState()->onStop()->onDestory()->onCreate()->onStart()->onRestoreInstanceState()->onResume()
3:横屏再切换回竖屏(和竖屏切换横屏一样)
onPause()->onSaveInstanceState()->onStop()->onDestory()->onCreate()->onStart()->onRestoreInstanceState()->onResume()
4:启动其他Activity:
onPause()->onSaveInstanceState()->onStop();
5:从其他Activity中返回:
onRestart()->onStart()->onResume()
6:启动对话框、关闭对话框,不调用上面列出的10个方法中任何一个
7:退出Activity:
onPause()->onStop()->onDestory()

posted on 2014-06-06 00:39  creasylai19  阅读(195)  评论(0编辑  收藏  举报