为应用中的Activity设置特定的方向是经常用到的办法,可以为我们省去不少不必要的麻烦。不过,我们今天讲的是屏幕方向改变时的生命周期,所以我们并不采用固定屏幕方向这种办法。
下面我们就结合实例讲解一下屏幕转换的生命周期,我们新建一个Activity命名为OrientationActivity,如下:
package com.scott.lifecycle;
import Android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
public class OrientationActivity extends Activity {
private static final String TAG = "OrientationActivity";
private int param = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orientation_portrait);
Log.i(TAG, "onCreate called.");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart called.");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart called.");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume called.");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause called.");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop called.");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestory called.");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("param", param);
Log.i(TAG, "onSaveInstanceState called. put param: " + param);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
param = savedInstanceState.getInt("param");
Log.i(TAG, "onRestoreInstanceState called. get param: " + param);
super.onRestoreInstanceState(savedInstanceState);
}
//当指定了android:configChanges="orientation"后,方向改变时onConfigurationChanged被调用
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged called.");
switch (newConfig.orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setContentView(R.layout.orientation_portrait);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(R.layout.orientation_landscape);
break;
}
}
}