Android强制创建选项卡活动?

发布于 2024-09-04 02:24:38 字数 789 浏览 1 评论 0原文

我有一个 Android 应用程序,其主要活动是 tabhost。我正在向选项卡主机添加多个选项卡,目的是进行多项活动。

我的问题是,在我单击选项卡之前,不会创建这些活动(不会调用 onCreate)。这是一个问题,因为我需要注册广播接收器:在打开特定选项卡之前可能会发送广播。

我尝试通过以下方法解决此问题:

  • 将接收器设置为静态并从其他地方注册。这是不可能的,因为我需要调用方法。
  • 调用 tabHost.setCurrentTabByTag(the_tag),然后切换回我的根选项卡。这只是有时有效,而且在我看来这是一个非常丑陋的解决方案。
  • 不使用活动,而只是使用视图。这也不是一个非常优雅的解决方案,因为它将我的 tabHost 活动变成一个巨大的大师类,执行各种不相关的事情。

解决这个问题的最佳方案是什么?我能否以某种方式使发送的广播“排队”,而不是在没有接收器时消失?我可以强制创建我的选项卡活动(这不是问题,我的用户几乎可以保证在某些时候使用每个选项卡)?还有其他想法吗?

编辑:

根据 CommonsWare 在这个问题中的说法; 从 Android TabHost 中的选项卡活动更新选项卡

它可能是将选项卡实现为视图而不是活动的更好主意...这在某种程度上改变了我的问题:

如何在同一活动中分离不同类型的逻辑?不太期待 30 多个方法的超级活动。

I have an Android application with a main activity that is the tabhost. I'm adding multiple tabs to the tabhost with an intent to several activities.

My problem is that these activities are not created (onCreate is not called) until I click on the tab. This is a problem because I need to register broadcastreceivers: there may be broadcasts that are sent -before- a particular tab is opened.

I tried to work around this by:

  • Setting my receivers as static and registering from somewhere else. This is not possible because I need to call into methods.
  • Calling into tabHost.setCurrentTabByTag(the_tag) and then switching back to my root tab. This only works sometimes and this is a very ugly solution imo.
  • Not using activities but just using views instead. Also not a very elegant solution because it turns my tabHost activity into one huge master class doing all kinds of unrelated things.

What would be the best solution to this problem? Can I somehow make sent broadcasts 'queue' instead of just disappearing when there are no receivers? Can I force creation of the my tab activities (this is not a problem, my users are almost guaranteed to use every single tab at some points)? Other ideas?

edit:

According to CommonsWare in this question;
Update tabs from a tab's activity in a Android TabHost

It may be a better idea to implement the tabs as views instead of activities... That somewhat changes my question:

How do I seperate different kinds of logic in the same activity? Not really looking forward to 30+ method uberactivity.

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

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

发布评论

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

评论(1

橘虞初梦 2024-09-11 02:24:38

我将使用 TabActivity 作为我的广播接收器,而不是选项卡活动。这将为您提供更清晰的代码并更好地控制如何处理广播

I would use the TabActivity as my broadcast receiver and not the tab activities. That would give you cleaner code and more control over how you handle the broadcasts

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