Android TabActivity 中奇怪的选项菜单行为

发布于 2024-08-30 07:59:53 字数 625 浏览 11 评论 0 原文

我有一个带有四个选项卡的 TabActivity (每个选项卡都有自己的 Activity)。每个选项卡都定义自己的 onCreateOptionsMenu(在某些情况下,还定义 onPrepareOptionsMenu)。加载每个选项卡时,将启动 AsyncTask 以检索填充该选项卡列表所需的数据。

如果我非常快速地在选项卡之间切换(当它们仍在加载时),然后按菜单按钮(当当前选项卡的 AsyncApiTask 仍在运行时),我可以获得错误的选项菜单出现。

例如,假设 FooActivity(选项卡 1)有一个带有“刷新”项的选项菜单,而 BarActivity(选项卡 2)有一个带有“查看全部”选项的选项菜单“ 物品。如果我启动应用程序(选项卡 1 处于活动状态),快速切换到选项卡 2,然后点击菜单,有时会显示“刷新”项目(而不是预期的“查看全部”项目)。

此外,虽然这种奇怪的行为有时只发生在第一次按下菜单时(后来按下显示正确的项目),但有时它会“卡住”,并且每次按下都会显示错误的项目,直到我切换选项卡。

知道会发生什么吗?我以前没有听说过这种情况,也找不到任何好的建议。

I have a TabActivity with four tabs (each is its own Activity). Each tab defines its own onCreateOptionsMenu (and in some cases, onPrepareOptionsMenu). When each tab is loaded, an AsyncTask is kicked off to retrieve the data needed to populate that tab's list.

If I switch between tabs very quickly (while they're still loading) and then press the menu button (while the current tab's AsyncApiTask is still running), I'm able to get the wrong options menu to appear.

For example, let's say FooActivity (tab 1) has an options menu with a "Refresh" item, and BarActivity (tab 2) has an options menu with a "View All" item. If I start the app (with tab 1 active), quickly switch to tab 2, and then hit menu, the "Refresh" item (rather than the expected "View All" item) will sometimes show.

Furthermore, while this weird behavior sometimes occurs just on the first menu press (and later presses show the right items), sometimes it gets "stuck", and the wrong items show up on every press until I switch tabs.

Any idea what could be going on? I haven't heard of this happening before, and haven't been able to find any good suggestions.

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

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

发布评论

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

评论(1

揽月 2024-09-06 07:59:53

是的,CommonsWare 的建议是更好的解决方案!您甚至可以考虑使用 FragmentTabHost 和每个选项卡的片段。这样,主机 Activity 就可以从 onTabChanged(String tab) 集中更新选项菜单。

虽然 AsyncTask 是从 Activity 生成的,但如果它尚未运行 onPostExecute(...),它仍然会运行。另外,由于 AsyncTask 的默认行为是可变的(不同版本的 android 会顺序/并行运行它们),因此如果没有您自己的 ThreadPoolExecutor。您的快速 Activity 切换和重复的 AsyncTask 似乎正在阻塞。

Yes, CommonsWare suggestion is a better solution! You could even consider using FragmentTabHost and fragments for each tab. That way the host activity can update the options menu centrally from onTabChanged(String tab).

While an AsyncTask is spawned from an Activity, if it hasn't run onPostExecute(...) it is still going. Also, since the default behavior of AsyncTasks is variable (different versions of android run them sequentially/parallel), it's hard to control without your own ThreadPoolExecutor. Your quick Activity switching and repeated AsyncTasks seem to be blocking.

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