【android】android中activity的生命周期

activity生命周期:

    

实例代码:

 1 public class DemoActivity extends Activity {
 2     
 3     //1、activity第一次被创建的时候,执行
 4     @Override
 5     public void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.main);
 8         System.out.println("onCreate");
 9         int page = 0;
10         if(savedInstanceState!=null){
11              page = savedInstanceState.getInt("page");
12              System.out.println("重新定位到:"+page+"页");//切换屏幕时,重新获取上次读取到的页码
13         }
14     }
15     //2、界面用户可见的时候,执行
16     @Override
17     protected void onStart() {
18         // TODO Auto-generated method stub
19         super.onStart();
20         System.out.println("onStart");
21     }
22     //3、界面上的按钮,文本等可被触摸时,执行
23     @Override
24     protected void onResume() {
25         // TODO Auto-generated method stub
26         super.onResume();
27         System.out.println("onResume");
28     }
29     
30     @Override
31     protected void onRestart() {
32         // TODO Auto-generated method stub
33         super.onRestart();
34         System.out.println("onRestart");
35     }
36     
37     //4、界面上的按钮,文本等不可被触摸时,执行
38     @Override
39     protected void onPause() {
40         // TODO Auto-generated method stub
41         super.onPause();
42         System.out.println("onPause");
43     }
44 
45     //在activity异常退出时,执行,保存信息到Bundle对象中去
46     @Override
47     protected void onSaveInstanceState(Bundle outState) {
48         super.onSaveInstanceState(outState);
49         outState.putInt("page", 250);//保存当前读取到的页码
50     }
51     //5、activity用户不可见
52     @Override
53     protected void onStop() {
54         // TODO Auto-generated method stub
55         super.onStop();
56         System.out.println("onStop");
57     }
58 
59     //6、activity被销毁的时候,执行
60     @Override
61     protected void onDestroy() {
62         // TODO Auto-generated method stub
63         super.onDestroy();
64         System.out.println("onDestroy");
65     }
66 
67 }

 

禁用android中activity进行屏幕切换

在AndroidManifest.xml的activity属性位置配置:

1 android:configChanges="keyboardHidden|orientation"

 

 

posted @ 2013-07-12 09:19  hacket520  阅读(207)  评论(0编辑  收藏  举报