如何处理 ProgressDialog 显示时的方向变化?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试在显示
ProgressDialog
期间禁用方向更改。在开始时执行:
并在完成后启用返回:
希望这有帮助。
(如果有人有合适的解决方案,我也会感兴趣:-)
You could try disabling orientation changes during the time you show the
ProgressDialog
.at the beginning do:
and enable back after completion:
Hope this helps.
(If anyone has a proper solution, I would also be interested :-)
您希望正确处理活动生命周期,这意味着保存和恢复活动的状态,而不是尝试阻止生命周期更改。阅读一些有关 AsyncTask 与 Activity 生命周期的文章。
例如: pause-and-resume-asynctasks-android 和 what-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.
的活动标记中
将其添加到应用程序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"/>
就我而言,我已经使用过
,但它对我不起作用
以下工作正常
In my case I have used
but it did not work for me
Following is working fine
您可以在清单中使用以下代码
You can use the following code in Your Manifest
尝试将此属性
android:configChanges="orientation"
添加到AndroidManifest.xml
文件中的Activity
元素。Try adding this attribute
android:configChanges="orientation"
to yourActivity
element in theAndroidManifest.xml
file.