屏幕旋转Activity的生命周期变化
屏幕旋转 Activity 的生命周期变化:
1、Activity 不重启请在 AndroidManifest.xml 设置如下:
......
???<activity android:name=".app.CustomDialogActivity"
??????????????? android:label="@string/activity_custom_dialog"
??????????????? android:configChanges="orientation|keyboardHidden">
......
2、如果不设置则启动如下:
第一次执行
onCreate(null)
onStart()
onPostCreate()
onResume()
onPostResume()
可将屏幕旋转以后:
onSaveInstanceState()
onPause()
onStop()
onDestroy()
onCreate(Bundle[{android:viewHierarchyState=Bundle[{android:views=android.util.SparseArray@43370a48}], key=123}])
onStart()
onRestoreInstanceState()
onPostCreate()
onResume()
onPostResume()
onDestroy() 证明杀掉了你的 activity, onCreate() 又重新启动了你的 activity
?
代码:
......
public void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.custom_dialog_activity);
??Log.w("Test", "onCreate(" + savedInstanceState + ")");
?}
?protected void onRestart() {
??super.onRestart();
??Log.w("Test", "onRestart()");
?}
?protected void onStart() {
??super.onStart();
??Log.w("Test", "onStart()");
?}
?protected void onRestoreInstanceState(Bundle savedInstanceState) {
??super.onRestoreInstanceState(savedInstanceState);
??Log.w("Test", "onRestoreInstanceState()");
?}
?protected void onPostCreate(Bundle savedInstanceState) {
??super.onPostCreate(savedInstanceState);
??Log.w("Test", "onPostCreate()");
?}
?protected void onResume() {
??super.onResume();
??Log.w("Test", "onResume()");
?}
?protected void onPostResume() {
??super.onPostResume();
??Log.w("Test", "onPostResume()");
?}
?protected void onSaveInstanceState(Bundle outState) {
??outState.putInt("key", 123);
??super.onSaveInstanceState(outState);
??Log.w("Test", "onSaveInstanceState()");
?}
?protected void onPause() {
??super.onPause();
??Log.w("Test", "onPause()");
?}
?protected void onStop() {
??super.onStop();
??Log.w("Test", "onStop()");
?}
?protected void onDestroy() {
??super.onDestroy();
??Log.w("Test", "onDestroy()");
?}
?protected void onNewIntent(Intent intent) {
??super.onNewIntent(intent);
??Log.w("Test", "onNewIntent()");
?}
.....