Android:完成后窗口泄漏
我已阅读其他窗口泄漏帖子并尝试了其中的建议,但无济于事。
我有 3 个活动:A、B 和 C。活动 A 从用户收集信息。活动 B 在与另一个线程上的服务器通信时显示旋转的 ProgressDialog。当线程完成时,它会关闭 ProgressDialog 并开始下一个活动。活动 C 将信息从服务器显示给用户。活动 B 的设置是为了当用户从 C 返回时,他们会返回到 A。
这些任务位于单独的活动中非常重要。
到目前为止,应用程序在大多数情况下成功执行了预期的操作,但以下情况除外:如果用户在返回活动 A 之前在活动 C 中更改方向,则应用程序会因窗口泄漏而崩溃。
- 我在 istart C 之前关闭 Activity B 的 onPause() 中的 ProgressDialog。
- 我尝试关闭 主线程上的 ProgressDialog 使用处理程序以及 单独的线程。
- 当用户不改变 C 方向,无窗户泄漏 发生。
有什么想法吗? TIA
I have read the other window leak posts and have tried what what suggested there to no avail.
I have 3 activities: A, B, and C. Activity A gathers information from the user. Activity B displays a spinning ProgressDialog while it communicates with a server on another thread. When the thread finishes, it dismisses the ProgressDialog and starts the next activity. Activity C displays the information from the server to the user. Activity B is set up so that when the user hits back from C, they fall back to A.
It is important that these tasks be in separate Activities.
As of now the app successfully does what it is supposed to in most cases, except in the following scenario: If the user changes the orientation while in activity C before returning to Activity A, the app crashes due to a window leak.
- I am dismissing the ProgressDialog inthe onPause() of Activity B before istart C.
- I have tried dismissing the
ProgressDialog on the main thread
using a handler as well as in the
separate thread. - When the user does not change the
orientation in C, no window leak
occurs.
Any ideas? TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种情况经常发生在使用 ProgressDialogs 的地方。我不久前尝试了 ProgressDialog,发现要做的事情是从 onPause() 中解雇它,并从 onResume() 中重新创建它。后台任务显然需要在您的 Activity & 中生存下来。对话框中,因此我使用 onRetainNonConfigurationInstance() 将任务实例从已销毁的 Activity 传递到新的 Activity。
另一种更欺骗的解决方法可能是简单地防止您的 Activity 仅仅因为屏幕方向发生变化而被销毁并重新创建。通过将 android:configChanges="orientation" 添加到 AndroidManifest.xml 中的标记来执行此操作。
This often happens where ProgressDialogs are used. I experimented with ProgressDialog a while back and found the thing to do was dismiss() it from onPause() and create it anew from onResume(). The background task obviously needs to survive your Activity & dialog so I used onRetainNonConfigurationInstance() to pass the task instance from the destroyed Activity to the new one.
An alternative, cheatier workaround might be to simply prevent your Activity from being destroyed and created anew merely because the screen orientation changed. Do this by adding android:configChanges="orientation" to the tag(s) in your AndroidManifest.xml.
我通过彻底改变处理一切的方式解决了我的问题。我现在只有两个活动(A 和 B),并在活动 B 中显示 ProgessDialog,同时根据需要处理 savingInstanceState 以解决该问题。
尽管我已经解决了我的应用程序上的问题,但我仍然不知道为什么以前会发生这种情况,并且想了解更多有关窗口泄漏以及为什么我遇到问题的信息。如果有人知道更多关于我遇到的问题,请发帖,因为我确信还有其他人也遇到同样的问题。
谢谢
I solved my problem by completely changing how i handled everything. I now have only two activities (A and B) and display the ProgessDialog in activity B while handling the savedInstanceState as needed in order to work around the problem.
Even though I have fixed the problem on my app, I still dont know why it was occuring before and would like to learn more about window leaks and why i was having problems. If anyone knows more about the problem i was having, please post as I'm sure there are others with the same problem.
Thanks
不确定这是否与您的具体问题有关,但我遇到了类似的问题,该问题与在活动的 onCreate 方法中创建的对话框的泄漏窗口有关。因此,如果您的活动启动时显示一个对话框,并且您进行了配置更改,操作系统会记住显示了哪些对话框,因此当活动被终止时,操作系统会记住显示的对话框。重新启动后,操作系统会尝试恢复旧对话框,而您的 Activity 会尝试再次显示相同的对话框(因为它位于 onCreate 中)。我发现,如果不是配置更改(即savedInstanceState!= null),则仅在onCreate期间显示对话框,泄漏窗口问题就消失了。
Not sure if this was related to your specific problem, but I had a similar issue which had to do with leaky windows for dialogs that are created in the onCreate method of an activity. So if your activity starts up showing a dialog, and you do a config change, the OS remembers which dialogs were shown, so when the activity is killed & restarted, the OS attempts to restore your old dialog while your activity tries to show the same dialog again (since it's in onCreate). I found by only showing the dialog during onCreate if it's not a config change (ie. savedInstanceState != null), the leaky window problem went away.