Android - 多级活动链的问题

发布于 2024-12-09 22:17:23 字数 1410 浏览 1 评论 0原文

(请注意,我已经在网上搜索了下面描述的警告,但几乎没有找到任何相关内容。)

我正在使用 API 级别 10。我有一个首选项屏幕(基于 XML),并且其中的选项之一创建一个自定义 ListActivity,如下所示:

  • PreferenceActivity 包含一个创建...的选项
    • ListActivity 这是一个对话框,使用...
      • setOnClickListener(),它包含一个 onClick() 方法,该方法(在调用 finish() 之前)将 startActivity() 一个新的 Intent...
        • 子活动启动...
          • AsyncTask 执行可变时间工作,完成后调用...
          • onPostExecute() 调用 finish()

事实是,它有效...但我我收到大量警告,开头是:

10-16 21:59:25.010: WARN/WindowManager(170): Rebuild removed 4 windows but added 3
10-16 21:59:25.010: WARN/WindowManager(170): This window was lost:.....

奇怪的是,这些警告仅在任务快速执行时才会出现!当我向 AsyncTask 添加 Thread.sleep() 调用以人为地增加其运行时间时,它起作用并且没有发出任何警告。事实上,只要运行时间超过(大约)500 毫秒,它就可以正常工作。 (请注意,我尝试使用 startActivityForResult() 并没有产生更大的效果 - 也会发生同样的问题。)


目标是用户选择一个首选项,他们更改其设置,进行一些处理,然后用户返回到他们开始使用偏好菜单。

我敢打赌这是一个竞争条件...窗口被销毁的顺序取决于运行时...并且我得到的印象是,当子 Activity 在其父 ListActivity 之前关闭时,会引发警告。但是,加入 1s sleep() 并不是一个合理的解决方案,除非这是某种 Android bug(不太可能,但今天我已经重现了其中的几个)。

那么,我的错误是什么导致了这一系列警告?最好说“根据偏好,做这个,然后做那个,然后完成”,但我认为我'我做的事情是等价的。也许不是……想法?


编辑:我决定尝试将此 ListActivity 作为自定义对话框...这是我最近尝试做的最痛苦的事情之一( getApplication() 不起作用,而且许多其他事情似乎都出了问题..在某种程度上可能是缺乏经验,但对话框实际上也不是为此而设计的......

(Note that I've searched online for the warnings I'm describing below, and have come up with next to nothing about them.)

I'm working with API level 10. I have a preference screen (XML-based), and one of the options in there creates a custom ListActivity as follows:

  • PreferenceActivity contains an option that creates a...
    • ListActivity which is a dialog that employs...
      • setOnClickListener() which contains an onClick() method that (right before calling finish()) will startActivity() a new Intent...
        • sub-Activity which starts up an...
          • AsyncTask which does variable time work which when done calls...
          • onPostExecute() which calls finish()

The thing is, it works... but I'm getting a raft of warning starting with:

10-16 21:59:25.010: WARN/WindowManager(170): Rebuild removed 4 windows but added 3
10-16 21:59:25.010: WARN/WindowManager(170): This window was lost:.....

Curiously, this raft of warnings ONLY comes up when the task executes quickly! When I added a Thread.sleep() call to my AsyncTask to artificially inflate its runtime it worked and threw no warnings whatsoever. In fact, as long as it takes more than (roughly) 500 ms to run it works fine. (Note that I tried using startActivityForResult() to no greater effect - the same problem occurs.)


The goal is that the user selects a preference item, they change its setting, some processing takes place, and then the user is left back at the preference menu they started on.

I'm betting it's a race condition... the order in which the windows are destroyed varies depending on that run-time... and I get the impression that when the sub-Activity closes before its parent ListActivity the warnings get thrown. But sprinkling a 1s sleep() in isn't a reasonable solution unless this is some sort of Android bug (unlikely, but then again I've reproduced a couple of those today already).

So, what's the flaw in this my that leads to this stream of warnings? It'd be nice to say "on preference, do this, then do that, then finish" but I think what I'm doing is the equivalent. Maybe not... thoughts?


Edit: I decided to try doing this ListActivity as a custom Dialog... that was one of the more painful things I've tried to do lately (getApplication() doesn't work and lots of other things seem to go wrong... it may be inexperience to some extent, but dialogs really weren't meant for this either...

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

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2024-12-16 22:17:23

请尝试以下两件事:

  • 在对其父活动 (PreferenceActivity) 调用 finish() 之前关闭对话框。

  • 确保您在子活动生命周期的后期启动 AsyncTask。我特别认为您应该在 onResume() 中启动它。

我最好的猜测是,在子活动有机会完全启动之前,AsyncTask 在子活动上调用 finish() 。为什么这很重要?我不知道。不过还是要尝试一下。祝你好运!

Try the following two things:

  • Dismiss your dialog before calling finish() on its parent activity (PreferenceActivity).

  • Make sure you are starting your AsyncTask later in the sub-activity's lifecycle. I'm specifically thinking you should launch it in onResume().

My best guess is that the AsyncTask is calling finish() on the sub-activity, before the sub-activity has had a chance to fully start up. Why that would matter? I'm not sure. Something to try though. Good luck!

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