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

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

	<activity android:name=".QTwitter" android:configChanges="orientation|keyboardHidden"
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

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
        }
    }

Android 屏幕旋转时保存状态

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.