Android 在旋转时保存某些值

发布于 2024-12-06 03:43:58 字数 331 浏览 1 评论 0原文

我有一个运行异步任务的活动,我不希望布局更改时重置所有内容。

我将其放入活动的清单元素 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橪书 2024-12-13 03:43:58

当您将 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 overriding onRestoreInstanceState(Bundle savedInstanceState) or checking bundle parameter at onCreate.

久随 2024-12-13 03:43:58

我决定发布对我有用的内容,这是你的样子吗?这是来自我的清单:

<activity android:name=".MediaSelectActivity" 
android:configChanges="orientation|keyboardHidden" 
android:label="Select Media Item for Vision"
 ></activity>

I decided to post what is working for me, is this what yours looks like? This is from my manifest:

<activity android:name=".MediaSelectActivity" 
android:configChanges="orientation|keyboardHidden" 
android:label="Select Media Item for Vision"
 ></activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文