Android屏幕旋转与acticity生命周期

  1. 设置 android:configChanges="orientation"和不设置这个属性,生命周期表现为重新创建activity

  2. 设置 android:configChanges="orientation|keyboardHidden",在2.3上表现为不重新创建activity,5.1如下

1
2
android:targetSdkVersion<=“12//生命周期表现为不重新创建activity
android:targetSdkVersion>”12//表现为重新创建activity
  1. 设置 android:configChanges="orientation|keyboardHidden|screenSize",在2.3和5.1上都表现为不重新创建,在屏幕旋转时,只会调用onConfigurationChanged方法

测试代码:

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