views:

318

answers:

1

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; http://stackoverflow.com/questions/2945274/android-tabhost-update-tabs-from-tabs-activity

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.

A: 

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

disretrospect
How would that solve anything though? I'm still left with uninitialized tab activities in which methods need to be called when broadcasts arrive.
Daniel
You should only try and call methods from a currently running activity. I don't think it is possible to have all activities from a tabhost initialized and running at the same time. It would be bad practice to do so as it would be a significant drain on the phone battery and processor. If a broadcast happens whilst your activity is paused or not yet initialized you could store it in your TabActivity and pass it through to the relevant activity only when that activity is resumed.
disretrospect