Android 在旋转时保存某些值
我有一个运行异步任务的活动,我不希望布局更改时重置所有内容。
我将其放入活动的清单元素 android:configChanges="orientation"
中,但不幸的是,活动甚至不再从横向布局切换到纵向布局!
我对 saveInstanceStates 和传递 Bundles 的了解只有一半,而且我绝对不确定如何将其与复杂的数据对象一起使用,那里的洞察力很受赞赏
我想做的是确保异步任务不会再次运行,(我可以将变量保存到磁盘,并在运行 asynctask 之前对它们进行条件检查)如果有一些 android 方法可以真正注意这一点,那就太好了!
谢谢
I have an activity that runs asynctasks among other things, I don't want everything to be reset when the layout changes.
I put this in the activity's manifest element android:configChanges="orientation"
but unfortunately the activity doesn't even switch from the landscape to portrait layout anymore when that is there!
I only halfway understand saveInstanceStates and passing Bundles around, and I'm definitely not sure how to use that with complex data objects, insight appreciated there
What I would like to do is make sure that the asynctasks don't run again, (I could save variables to disk, and do a conditional check for them before running the asynctask) if there is some android way to really watch out for this that would be great!
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这个库一定能满足您的目的
http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/
I believe this library will definitely serve your purpose
http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/
当您将
android:configChanges="orientation"
放入清单中时,您就声明 android 不需要处理方向更改。因此,定向永远不会发生。您可以在此线程中查看如何将值保存到捆绑 onConfigurationChange(在您的情况下为方向)。如何保存 Android 应用程序的状态?
简而言之,您可以通过重写
onSaveInstanceState(Bundle savingInstanceState)
来保存一些要捆绑的值。通过重写onRestoreInstanceState(Bundle savingInstanceState)
或检查onCreate
处的捆绑包参数来检索保存到捆绑包的值。When you put
android:configChanges="orientation"
in the manifest, you are declaring that android won't need to handle orientation change. Thus, the orientation never happens.You can check out how to save value to bundle onConfigurationChange(in your case, orientation) in this thread.How do I save an Android application's state?
In brief, you save some value to bundle by overriding
onSaveInstanceState(Bundle savedInstanceState)
. Retrieve the value you saved to bundle by either overridingonRestoreInstanceState(Bundle savedInstanceState)
or checking bundle parameter atonCreate
.我决定发布对我有用的内容,这是你的样子吗?这是来自我的清单:
I decided to post what is working for me, is this what yours looks like? This is from my manifest: