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; 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.

1 Answer 1

0

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

3
  • How would that solve anything though? I'm still left with uninitialized tab activities in which methods need to be called when broadcasts arrive. Jun 3, 2010 at 15:40
  • 1
    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.
    – m6tt
    Jun 3, 2010 at 15:55
  • @disretrospect Could you please show example how can we pass the data from TabActivity to relevant childActivity when it's created/resumed? Thx
    – zeratul021
    Aug 30, 2011 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.