Jianqing's Blog Archive

Android 屏幕旋转时保存状态

默认情况下我们在旋转Android屏幕时,会重新跑”onCreate”相当于会重新启动程序. 很多时候我们不希望它”重新启动”程序.用下面的方法就可以达到这种效果.

1.在manifest里加上这句android:configChanges=”orientation|keyboardHidden”


	
			
				
				
			
		

2.在这个Activity里重写下面这个函数:


   /**
     * Used to  skip reOncreate when the screen rotated.
     */
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // land
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            // port
        }
    }

Tags: