onDestroy-->onCreate-->onStart-->onRestoreInstanceState-->onResume-->
當橫屏再切換成豎屏時:會調用兩次上述過程:
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
等等之類的。我就不多說了。
古畫說的好,jue知此事要躬行,so:
下面是我自己的研究結果:
代碼先呈上:
MainActivity.java
package com.example.liferecycletest;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final String TAG = "LifeRecycleTest";
TextView tv = null;
TextView tv_ = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showLog("onCreate");
tv = (TextView) findViewById(R.id.hello);
tv_ = (TextView) findViewById(R.id.hello_);
findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv.setText("Just Changed");
tv_.setText("Just Changed");
}
});
}
public void showLog(String info) {
Log.e(TAG, info);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
showLog("onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
showLog("onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
showLog("onRestart");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
tv.setText(savedInstanceState.getString("text"));
}
showLog("onResoreInstanceState");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
showLog("onResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
showLog("onSaceInstanceState");
outState.putString("text", "Just Change");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
showLog("onConfigurationChanged");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
showLog("onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
showLog("onStop");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.liferecycletest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
現在很情況說下:
A.在沒有設置configchanges的情況下:
豎——>橫屏幕的切換Log:
橫——>豎屏幕切換Log:
可以發現這兩個是一樣的,剛看到有人說不一樣當時就想不通了,
完全等價的過程,Android爲什麼要設計的不一樣呢。親自實驗一下發現是一樣的。
B.經過本人發現,必須在configchanges中設置”orientation|screenSize“同時存在,這個時候,不管怎麼切換Activity都不會重新創建,走之前的流程了,只是會調用onConfigurationChanged()函數而已:
親自實驗就是這樣了,歡迎大家指正。