Managing Android services in tabhosts or custom contexts?
I have a tabhost with a number of tabs that all need access to the same service through out the life of the application. 'Best practice' suggests that you unbind services when pausing an activity, but this would mean disconnecting from the service, only to reconnect on selection of the next tab, which is daft (not least because my service maintains a connection to a server). I could store a reference to the service in the tabhost, but I don't know how to get a reference to the tabhost from its child activities. The alternative is to extend the Application class and manage the service there.
Does anyone have开发者_高级运维 any better ideas or reasons why I shouldn't do either of the above?
Thanks
Extending the Application class is very good alternative.
However you can extend Activity and make all your child activities to inherit from your extended activity, as this way you will implement the same code (service handling) in 1 activity(the parent).
Activity -> MyParentActivity -> MyChildActivity1
精彩评论