android-----横竖屏切换对Activity生命周期的影响

       前面我们分析过Activity的生命周期(见:android-----Activity生命周期),但是在现实应用中我们可能会对手机进行横竖屏切换,那么这样的切换对Activity的生命周期有什么影响呢?

       先来看看实例:

public class Activity1 extends Activity{
	public Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lifecycle);
		System.out.println("Activity1----->onCreate");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.out.println("Activity1----->onDestroy");
	}

	@Override
	protected void onPause() {
		super.onPause();
		System.out.println("Activity1----->onPause");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		System.out.println("Activity1----->onRestart");
	}

	@Override
	protected void onResume() {
		super.onResume();
		System.out.println("Activity1----->onResume");
	}

	@Override
	protected void onStart() {
		super.onStart();
		System.out.println("Activity1----->onStart");
	}

	@Override
	protected void onStop() {
		super.onStop();
		System.out.println("Activity1----->onStop");
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		System.out.println("Activity1----->onRestoreInstanceState");
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		System.out.println("Activity1----->onSaveInstanceState");
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
		{
			System.out.println("现在是横屏转竖屏");
		}else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
			System.out.println("现在是竖屏转横屏");
		}
	}
}

       横竖屏切换涉及到的是Activity的android:configChanges属性,而与其android:launchMode属性没有关系;

       android:configChanges可以设置的属性值有:

       (1)orientation:消除横竖屏的影响

       (2)keyboardHidden:消除键盘的影响

       (3)screenSize:消除屏幕大小的影响

       当我们设置Activity的android:configChanges属性为orientation或者orientation|keyboardHidden或者不设置这个属性的时候,Logcat输出如下:

       刚刚启动Activity的时候:

       Activity1----->onCreate
       Activity1----->onStart
       Activity1----->onResume

       模拟器中按下Ctrl+F11或者Ctrl+F12由竖屏切换到横屏:

       Activity1----->onPause
       Activity1----->onSaveInstanceState
       Activity1----->onStop
       Activity1----->onDestroy
       Activity1----->onCreate
       Activity1----->onStart
       Activity1----->onRestoreInstanceState
       Activity1----->onResume

       模拟器中按下Ctrl+F11或者Ctrl+F12由横屏切换到竖屏:

       Activity1----->onPause
       Activity1----->onSaveInstanceState
       Activity1----->onStop
       Activity1----->onDestroy
       Activity1----->onCreate
       Activity1----->onStart
       Activity1----->onRestoreInstanceState
       Activity1----->onResume

       当我们设置Activity的android:configChanges属性为orientation|screenSize或者orientation|screenSize|keyboardHidden,Logcat输出如下:

       刚刚启动Activity的时候:

       Activity1----->onCreate
       Activity1----->onStart
       Activity1----->onResume

       模拟器中按下Ctrl+F11或者Ctrl+F12由竖屏切换到横屏:
       现在是竖屏转横屏

       模拟器中按下Ctrl+F11或者Ctrl+F12由横屏切换到竖屏:

       现在是横屏转竖屏

       可以发现在设置了orientation|screenSize属性之后,在进行横竖屏切换的时候调用的方法是onConfigurationChanged,而不会重新加载Activity的各个生命周期;

       还有一点需要注意的是在进行横竖屏切换的时候在调用onStop之前会调用onSaveInstanceState来进行Activity的状态保存,随后在重新显示该Activity的onResume方法之前会调用onRestoreInstanceState来恢复之前由onSaveInstanceState保存的Activity信息;

       当然在显示中我们可以屏蔽掉横竖屏的切换操作,这样就不会出现切换的过程中Activity生命周期重新加载的情况了,具体做法是,在Activity中加入如下语句:

      android:screenOrientation="portrait" 始终以竖屏显示 
       android:screenOrientation="landscape" 始终以横屏显示

       如果不想设置整个软件屏蔽横竖屏切换,只想设置屏蔽某个Activity的横竖屏切换功能的话,只需要下面操作:

       Activity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);以竖屏显示

       Activity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);以横屏显示


     


 

你可能感兴趣的:(android,android,Activity生命周期)