Android force tab activity creation?
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.
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
精彩评论