如何防止设备方向更改时活动仅在一段时间内而不是一直重新启动

发布于 2025-01-07 04:41:08 字数 552 浏览 1 评论 0原文

在我的活动中,oncreate() 中完成了很多处理,例如打开数据库、在 SD 卡上写入文件。但所有这些繁重的处理仅在活动第一次启动时发生一次,之后就不再发生。我正在使用异步任务来实现此目的。

我使用以下代码来防止设备配置更改期间活动重新启动。

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }

我希望仅在初始设置发生时因设备方向更改而禁用活动重新启动,之后我想启用活动重新启动。知道我该如何实现这一目标吗?

In my activity, there is a lot of processing done in oncreate() like opening a database, writing files on the sd card. But all of this heavy processing happens only once when the activity is launched the first time and not after that. I am using an Async task to achieve this.

I am using the following code to prevent activity restart during device configuration change.

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }

I wish to disable activity restart due to change in device orientation only once when the initial setup takes place and after that, I want to enable activity restart. Any idea how I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

离鸿 2025-01-14 04:41:08

他们说:“如果你不能打败他们,那就加入他们。”因此,您应该更好地组织您的应用程序工作流程。

将与 UI 相关的操作保留在标准生命周期方法(onCreate、onStart 等)中,并要求其他线程进行持久操作。

请查看 AsyncTask 和 IntentService 类,它们对您非常有用。

总而言之,使代码适应底层系统通常比强迫系统本身遵循您的意愿更容易

"if you can't beat them, join them" they said. Therefore, you should better organize you application workflow.

Keep your UI-related operations in the standard lifecycle methods (onCreate, onStart, etc.) and demand the long-lasting operation to other threads.

Please take a look to AsyncTask and IntentService classes, they will be very useful to you.

To summarize, it is often easier to adapt your code to the underlying system than forcing the system itself to follow your will

忆沫 2025-01-14 04:41:08

将其添加到清单文件

android:configChanges="orientation|keyboardHidden"中的活动标记中

add this to your activity tag in manifest file

android:configChanges="orientation|keyboardHidden"

萌酱 2025-01-14 04:41:08

最简单的方法是调用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)。不过我最好考虑一下 STT LCU 的说法。我建议从以下一些文章开始:

http://developer. android.com/guide/topics/resources/runtime-changes.html

http://www.vogella.de/articles/AndroidPerformance/article.html#lifecycle

The simplest way would be calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR). However I'd better consider what STT LCU is saying. Here are some articles I'd suggest start with:

http://developer.android.com/guide/topics/resources/runtime-changes.html

http://www.vogella.de/articles/AndroidPerformance/article.html#lifecycle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文