Android 最基础生命周期及旋转屏幕问题

 1 public class MainActivity extends Activity {
 2     
 3     private static final String TAG ="MainActivity";
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7 
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         Log.d(TAG, "onCreate");
11     }
12 
13     @Override
14     protected void onStart() {
15 
16         super.onStart();
17         Log.d(TAG, "onStart");
18     }
19 
20     @Override
21     protected void onRestart() {
22 
23         super.onRestart();
24         Log.d(TAG, "onRestart");
25     }
26 
27     @Override
28     protected void onResume() {
29 
30         super.onResume();
31         Log.d(TAG, "onResume");
32     }
33 
34     @Override
35     protected void onPause() {
36 
37         super.onPause();
38         Log.d(TAG, "onPause");
39     }
40 
41     @Override
42     protected void onStop() {
43 
44         super.onStop();
45         Log.d(TAG, "onStop");
46     }
47 
48     @Override
49     protected void onDestroy() {
50 
51         super.onDestroy();
52         Log.d(TAG, "onDestroy");
53     }
54 
55     @Override
56     public void onConfigurationChanged(Configuration newConfig) {
57 
58         super.onConfigurationChanged(newConfig);
59         Log.d(TAG, "onConfigurationChanged");
60     }
View Code

Activity各个生命周期

启动:

按返回键:

按home键

按home键后再次从图标启动

竖屏切横屏:

横屏切竖屏:

在AndroidManifest.xml 添加

android:configChanges="orientation" 

 切屏时和不加一样

根据Android SDK描述,在API13及以上还要加上screenSize。因为横竖屏变化,屏幕横竖尺寸也变化

android:configChanges="orientation|keyboardHidden|screenSize"

 

"orientation" 	The screen orientation has changed — the user has rotated the device.

Note: If your application targets API level 13 or higher 
(as declared by the minSdkVersion and targetSdkVersion attributes),
then you should also declare the "screenSize" configuration,
because it also changes when a device switches between portrait and landscape orientations.

然后再Activity中添加方法:

@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.d(TAG, "onConfigurationChanged");
		super.onConfigurationChanged(newConfig);
		
	}

 那么再旋转屏幕时,就会执行onConfigurationChanged方法

改变Activity的配置可能会使Activity重启动(默认情况下会重启),如果不想重新启动就需要配置configChanges属性,它可以配置多个用“|”分格。

当 在activity加上android:configChanges="keyboardHidden|orientation"属性,就不会重启 activity.而只是调用onConfigurationChanged(Configuration newConfig).这样就可以在这个方法里调整显示方式.

在xml文件里面可以进行配置configChanges也可以在代码中动态配置

android:configChanges=["mcc", "mnc", "locale",
                                 "touchscreen", "keyboard", "keyboardHidden",
                                 "navigation", "screenLayout", "fontScale",
                                 "uiMode", "orientation", "screenSize",
                                 "smallestScreenSize"]

 还有屏幕旋转的相关配置:android:screenOrientation 这个属性是控制Activity启动时候的方向

android:screenOrientation=["unspecified" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "userLandscape" | "userPortrait" |
                                     "sensor" | "fullSensor" | "nosensor" |
                                     "user" | "fullUser" | "locked"]

android:screenOrientation="landscape"---控制Acitvity为横屏显示。

android:screenOrientation="portrait"---控制Activity为竖屏显示

unspecified--系统默认显示

user---用户当前的首选方向

behind---与当前堆栈下面的Activity同向

sensor---根据传感器定(常用)

nosensor---忽略传感器;系统根据unspecified显示

 

posted @ 2015-12-18 11:20  千古明  阅读(882)  评论(0编辑  收藏  举报