在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失。
因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方法:
<!-- screenOrientation: landscape是横屏,portrait是竖屏 -->
<!-- 低版本横竖屏切换禁用重新创建activity:configChanges="orientation|keyboardHidden -->
<!-- 以下为高版本(4.0以上)禁用重新创建activity的配置: -->
<activity android:name="MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>