旋转屏幕重新加载Activity

 在启动一个acitivity之后,旋转一下屏幕(从竖屏模式切换到横屏模式,或者反过来),尤其是带有侧滑键盘的机型打开滑动键盘时,如果屏幕为竖屏模式,则屏幕会被强制切换到横屏模式。当屏幕显示模式发生变化时,由于横竖屏显示模式存在一些明显的区别,Android系统会认为当前activity显示内容已经不再适合新的屏幕显示模式,所以它会重新加载当前的activity,选择合适的资源以适应新的屏幕显示模式。下面通过一个简单的例子跟踪Acitivity生命周期的变化。

        新建一个工程,包含两个Activity分别为DemoParentActivity和DemoChildActivity,在第一个Activity中添加一个可以跳转到第二个Activity的按钮,并分别实现它们的生命周期函数,在函数里面打印一些日志信息,观察Activity生命周期函数调用情况。

        ActivityTestActivity.class的代码:
01	package com.devdiv.test.activitytest;
02	 
03	 
04	import android.app.Activity;
05	import android.content.Intent;
06	import android.content.res.Configuration;
07	import android.os.Bundle;
08	import android.util.Log;
09	import android.view.View;
10	 
11	public class ActivityTestActivity extends Activity {
12	    /** Called when the activity is first created. */
13	    @Override
14	    public void onCreate(Bundle savedInstanceState) {
15	        super.onCreate(savedInstanceState);
16	        setContentView(R.layout.main);
17	         
18	         
19	        if (savedInstanceState == null) {
20	                Log.d("test", "ActivityTestActivity - onCreate()");
21	                } else {
22	                        Log.d("test","ActivityTestActivity - onCreate() with a supplied savedInstanceState");
23	                }
24	    }
25	 
26	        @Override
27	        public void onConfigurationChanged(Configuration newConfig) {
28	                // TODO Auto-generated method stub
29	                super.onConfigurationChanged(newConfig);
30	                Log.d("test", "ActivityTestActivity - onConfigurationChanged()");
31	        }
32	 
33	        @Override
34	        protected void onDestroy() {
35	                // TODO Auto-generated method stub
36	                super.onDestroy();
37	                Log.d("test", "ActivityTestActivity - onDestroy()");
38	        }
39	 
40	        @Override
41	        protected void onPause() {
42	                // TODO Auto-generated method stub
43	                super.onPause();
44	                Log.d("test", "ActivityTestActivity - onPause()");
45	        }
46	 
47	        @Override
48	        protected void onRestart() {
49	                // TODO Auto-generated method stub
50	                super.onRestart();
51	                Log.d("test", "ActivityTestActivity - onRestart()");
52	        }
53	 
54	        @Override
55	        protected void onRestoreInstanceState(Bundle savedInstanceState) {
56	                // TODO Auto-generated method stub
57	                super.onRestoreInstanceState(savedInstanceState);
58	                Log.d("test", "ActivityTestActivity - onRestoreInstanceState()");
59	        }
60	 
61	        @Override
62	        protected void onResume() {
63	                // TODO Auto-generated method stub
64	                super.onResume();
65	                Log.d("test", "ActivityTestActivity - onResume()");
66	        }
67	 
68	        @Override
69	        protected void onSaveInstanceState(Bundle outState) {
70	                // TODO Auto-generated method stub
71	                super.onSaveInstanceState(outState);
72	                Log.d("test", "ActivityTestActivity - onSaveInstanceState()");
73	        }
74	 
75	        @Override
76	        protected void onStart() {
77	                // TODO Auto-generated method stub
78	                super.onStart();
79	                Log.d("test", "ActivityTestActivity - onStart()");
80	        }
81	 
82	        @Override
83	        protected void onStop() {
84	                // TODO Auto-generated method stub
85	                super.onStop();
86	                Log.d("test", "ActivityTestActivity - onStop()");
87	        }
88	         
89	        public void onButtonClick(View v) {
90	                Intent mIntent = new Intent(this, SecondActivity.class);
91	                startActivity(mIntent);
92	        }
93	     
94	     
95	}

SecondActivity.class的代码:

01	package com.devdiv.test.activitytest;
02	 
03	 
04	import android.app.Activity;
05	import android.content.Intent;
06	import android.content.res.Configuration;
07	import android.os.Bundle;
08	import android.util.Log;
09	import android.view.View;
10	 
11	public class ActivityTestActivity extends Activity {
12	    /** Called when the activity is first created. */
13	    @Override
14	    public void onCreate(Bundle savedInstanceState) {
15	        super.onCreate(savedInstanceState);
16	        setContentView(R.layout.main);
17	         
18	         
19	        if (savedInstanceState == null) {
20	                Log.d("test", "ActivityTestActivity - onCreate()");
21	                } else {
22	                        Log.d("test","ActivityTestActivity - onCreate() with a supplied savedInstanceState");
23	                }
24	    }
25	 
26	        @Override
27	        public void onConfigurationChanged(Configuration newConfig) {
28	                // TODO Auto-generated method stub
29	                super.onConfigurationChanged(newConfig);
30	                Log.d("test", "ActivityTestActivity - onConfigurationChanged()");
31	        }
32	 
33	        @Override
34	        protected void onDestroy() {
35	                // TODO Auto-generated method stub
36	                super.onDestroy();
37	                Log.d("test", "ActivityTestActivity - onDestroy()");
38	        }
39	 
40	        @Override
41	        protected void onPause() {
42	                // TODO Auto-generated method stub
43	                super.onPause();
44	                Log.d("test", "ActivityTestActivity - onPause()");
45	        }
46	 
47	        @Override
48	        protected void onRestart() {
49	                // TODO Auto-generated method stub
50	                super.onRestart();
51	                Log.d("test", "ActivityTestActivity - onRestart()");
52	        }
53	 
54	        @Override
55	        protected void onRestoreInstanceState(Bundle savedInstanceState) {
56	                // TODO Auto-generated method stub
57	                super.onRestoreInstanceState(savedInstanceState);
58	                Log.d("test", "ActivityTestActivity - onRestoreInstanceState()");
59	        }
60	 
61	        @Override
62	        protected void onResume() {
63	                // TODO Auto-generated method stub
64	                super.onResume();
65	                Log.d("test", "ActivityTestActivity - onResume()");
66	        }
67	 
68	        @Override
69	        protected void onSaveInstanceState(Bundle outState) {
70	                // TODO Auto-generated method stub
71	                super.onSaveInstanceState(outState);
72	                Log.d("test", "ActivityTestActivity - onSaveInstanceState()");
73	        }
74	 
75	        @Override
76	        protected void onStart() {
77	                // TODO Auto-generated method stub
78	                super.onStart();
79	                Log.d("test", "ActivityTestActivity - onStart()");
80	        }
81	 
82	        @Override
83	        protected void onStop() {
84	                // TODO Auto-generated method stub
85	                super.onStop();
86	                Log.d("test", "ActivityTestActivity - onStop()");
87	        }
88	         
89	        public void onButtonClick(View v) {
90	                Intent mIntent = new Intent(this, SecondActivity.class);
91	                startActivity(mIntent);
92	        }
93	     
94	     
95	}



        SecondActivity.class的代码:

01	package com.devdiv.test.activitytest;
02	 
03	import android.app.Activity;
04	import android.content.res.Configuration;
05	import android.os.Bundle;
06	import android.util.Log;
07	 
08	public class SecondActivity extends Activity {
09	         
10	        /** Called when the activity is first created. */
11	    @Override
12	    public void onCreate(Bundle savedInstanceState) {
13	        super.onCreate(savedInstanceState);
14	        setContentView(R.layout.second);
15	         
16	         
17	        if (savedInstanceState == null) {
18	                Log.d("test", "SecondActivity - onCreate()");
19	                } else {
20	                        Log.d("test","SecondActivity - onCreate() with a supplied savedInstanceState");
21	                }
22	    }
23	 
24	        @Override
25	        public void onConfigurationChanged(Configuration newConfig) {
26	                // TODO Auto-generated method stub
27	                super.onConfigurationChanged(newConfig);
28	                Log.d("test", "SecondActivity - onConfigurationChanged()");
29	        }
30	 
31	        @Override
32	        protected void onDestroy() {
33	                // TODO Auto-generated method stub
34	                super.onDestroy();
35	                Log.d("test", "SecondActivity - onDestroy()");
36	        }
37	 
38	        @Override
39	        protected void onPause() {
40	                // TODO Auto-generated method stub
41	                super.onPause();
42	                Log.d("test", "SecondActivity - onPause()");
43	        }
44	 
45	        @Override
46	        protected void onRestart() {
47	                // TODO Auto-generated method stub
48	                super.onRestart();
49	                Log.d("test", "SecondActivity - onRestart()");
50	        }
51	 
52	        @Override
53	        protected void onRestoreInstanceState(Bundle savedInstanceState) {
54	                // TODO Auto-generated method stub
55	                super.onRestoreInstanceState(savedInstanceState);
56	                Log.d("test", "SecondActivity - onRestoreInstanceState()");
57	        }
58	 
59	        @Override
60	        protected void onResume() {
61	                // TODO Auto-generated method stub
62	                super.onResume();
63	                Log.d("test", "SecondActivity - onResume()");
64	        }
65	 
66	        @Override
67	        protected void onSaveInstanceState(Bundle outState) {
68	                // TODO Auto-generated method stub
69	                super.onSaveInstanceState(outState);
70	                Log.d("test", "SecondActivity - onSaveInstanceState()");
71	        }
72	 
73	        @Override
74	        protected void onStart() {
75	                // TODO Auto-generated method stub
76	                super.onStart();
77	                Log.d("test", "SecondActivity - onStart()");
78	        }
79	 
80	        @Override
81	        protected void onStop() {
82	                // TODO Auto-generated method stub
83	                super.onStop();
84	                Log.d("test", "SecondActivity - onStop()");
85	        }
86	         
87	 
88	}

 完成了工程代码之后,在eclipse中直接运行工程启动ActivityTestActivity,然后旋转手机(模拟器可以通过CTR+F11组合键进行横竖屏切换),可以从下图打印的日志中看到,当旋转手机屏幕后,DemoParentActivity被销毁后,又被重新加载了一次,同时还可以看到Activity的onSaveInstanceState和onRestoreInstanceState方法也被调用了,这就说明需要在切换屏幕显示模式时,通过这两个方法保存和恢复临时数据。

旋转屏幕重新加载Activity_第1张图片

                                                        图1  模拟器LogCat日志信息
        某些情况下,并不希望在旋转屏幕时Activity被重新加载,这时可以通过修改Android应用工程的Androidmanifest.xml文件中Activity节点的android:configChanges属性,通常需要把这个属性设置为android:configChanges="orientation|navigation|keyboardHidden",这表示当屏幕旋转,页面转换以及键盘隐藏时都不要重新加载Activity,而是调用onConfigurationChanged方法。在上面的代码中可以看到SecondActivity覆盖了Activity的onConfigurationChanged方法,并设置了activity的android:configChanges属性。

        接下来点击按钮来启动SecondActivity,然后旋转屏幕再看一下输出的日志信息,从图2-11中可以清楚的看到,当切换屏幕显示模式时,SecondActivity并没有被重新加载,只是调用了它的onConfigurationChanged()方法,如果并不希望在旋转屏幕时Activity被重新加载,那么需要实现Activity的onConfigurationChanged方法进行添加相应的的处理逻辑。


图2  模拟器LogCat日志信息



        旋转手机屏幕只会造成当前Activity的重新加载,并不会重新加载整个应用程序。现在有一个问题需要作进一步的分析,就是在旋转手机屏幕后,会不会影响Activity栈中其他的activity。在上面应用程序中,当旋转手机屏幕时,SecondActivity的onConfigurationChanged方法被调用,此时可以进行以下两种不同的操作:

        1、通过返回键,返回到ActivityTestActivity
        2、再次旋转屏幕之后,通过返回键,返回到ActivityTestActivity

        读者可以自行通过查看日志的方式验证,不再赘述。
        ActivityTest工程: DEMO

你可能感兴趣的:(旋转屏幕重新加载Activity)