当我旋转手机时,活动会再次调用onCreate并重置所有内容。是否有可能保存像位置管理器这样的数据和服务,或者我必须保存和恢复它?所谓数据,我指的是类似于对象列表的东西。如何防止代码在每次旋转手机时继续运行onCreate?
发布于 2015-04-03 22:23:46
将此添加到Activity中的AndroidManifest.xml中
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
示例片段:
<activity
android:name="your.package.activity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" <!-- Keep data on rotation -->
android:windowSoftInputMode="adjustPan"> <!-- Keep keyboard from "pushing" layout -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>注意:这不是“首选”方式,但我使用的大多数应用程序(在这里阅读:)
https://stackoverflow.com/questions/29439491
复制相似问题