如何处理 ProgressDialog 显示时的方向变化?

发布于 2024-09-19 00:18:50 字数 390 浏览 10 评论 0原文

我在 AsyncTask 对象的 onPreExecute 方法中显示 ProgressDialog,并在 onPostExecute 方法中取消 ProgressDialog。在 doInBackground 方法中,我发出 HTTP 请求以进行用户注册。我希望允许更改屏幕方向。当我在 doInBackground 方法仍在运行时更改方向时,我收到各种有趣的错误,例如“IllegalArgumentException:视图未附加到窗口管理器”和“RegisterScreen 已泄漏窗口...”

怎么办?方向更改后我可以正确继续显示 ProgressDialog 吗?或者,在用户请求提交注册后,如何禁用方向更改?

I am showing a ProgressDialog in the onPreExecute method of an AsyncTask object and canceling the ProgressDialog in the onPostExecute method. In the doInBackground method I am making an HTTP request for user registration. I wish to allow screen orientation changes. When I change the orientation while the doInBackground method is still running, i get all sorts of fun errors like 'IllegalArgumentException: View not attached to window manager' and 'RegisterScreen has leaked window...'

How can I properly continue to show the ProgressDialog after an orientation change? Or maybe, how can I disable orientation change after the user requests to submit their registration?

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

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

发布评论

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

评论(6

童话 2024-09-26 00:18:51

您可以尝试在显示 ProgressDialog 期间禁用方向更改。

在开始时执行:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

并在完成后启用返回:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

希望这有帮助。

(如果有人有合适的解决方案,我也会感兴趣:-)

You could try disabling orientation changes during the time you show the ProgressDialog.

at the beginning do:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and enable back after completion:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Hope this helps.

(If anyone has a proper solution, I would also be interested :-)

瑾兮 2024-09-26 00:18:51

您希望正确处理活动生命周期,这意味着保存和恢复活动的状态,而不是尝试阻止生命周期更改。阅读一些有关 AsyncTask 与 Activity 生命周期的文章。

例如: pause-and-resume-asynctasks-androidwhat-to-do-with-asynctask-in-onpause

You want to properly handle the activity lifecycle, which means saving and restoring the state of your activity, not attempting to prevent lifecycle changes. Do some reading on AsyncTask vs. the activity lifecycle.

For example: pause-and-resume-asynctasks-android and what-to-do-with-asynctask-in-onpause.

计㈡愣 2024-09-26 00:18:51

的活动标记中

将其添加到应用程序manifest.xml

Add this in activity tag in application manifest.xml

<activity android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:name=".your.package"/>

花开浅夏 2024-09-26 00:18:51

就我而言,我已经使用过

android:configChanges="orientation" 

,但它对我不起作用

以下工作正常

<activity android:name=".MyActivity" 
          android:configChanges="orientation|screenSize|screenLayout">
</activity>

In my case I have used

android:configChanges="orientation" 

but it did not work for me

Following is working fine

<activity android:name=".MyActivity" 
          android:configChanges="orientation|screenSize|screenLayout">
</activity>
毁梦 2024-09-26 00:18:51

您可以在清单中使用以下代码

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name=".your.package">

You can use the following code in Your Manifest

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name=".your.package">
无法回应 2024-09-26 00:18:50

尝试将此属性 android:configChanges="orientation" 添加到 AndroidManifest.xml 文件中的 Activity 元素。

Try adding this attribute android:configChanges="orientation" to your Activity element in the AndroidManifest.xml file.

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