相較於 onRetainNonConfigurationInstance() 和 getLastNonConfigurationInstance()來控制Activity資料的保存,另一個選擇方式是改寫 AndroidManifest.xml裡面Activity的Configuration屬性。正常情況下,執行Activity然後旋轉方向後的 Life Cycle如下:
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onResume()
然後我們改變Configuration後如下:
<activity android:name=".Main"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
如此一來,執行程式後,再把畫面旋轉或是開啟硬體鍵盤後的Life Cycle如下:
onCreate()
onStart()
onResume()
也就是不會消滅後再重新啟動。如果想要在旋轉畫面後做一些特別的事情,可以@Override
onConfigurationChanged() 如下:
public class Main extends Activity {
/** Called when the activity is first created. */
private final static String TAG = "TestLife";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d(TAG, "landscape");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Log.d(TAG, "portrait");
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Log.d(TAG, "keyboard visible");
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Log.d(TAG, "keyboard hidden");
}
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
@Override
public void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart()");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}
其它需要注意的事項請參考官網說明,另外Configuration的其他選項還有這些可以使用。
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onResume()
然後我們改變Configuration後如下:
<activity android:name=".Main"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
如此一來,執行程式後,再把畫面旋轉或是開啟硬體鍵盤後的Life Cycle如下:
onCreate()
onStart()
onResume()
也就是不會消滅後再重新啟動。如果想要在旋轉畫面後做一些特別的事情,可以@Override
onConfigurationChanged() 如下:
public class Main extends Activity {
/** Called when the activity is first created. */
private final static String TAG = "TestLife";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d(TAG, "landscape");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Log.d(TAG, "portrait");
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Log.d(TAG, "keyboard visible");
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Log.d(TAG, "keyboard hidden");
}
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
@Override
public void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart()");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}
其它需要注意的事項請參考官網說明,另外Configuration的其他選項還有這些可以使用。
沒有留言:
張貼留言