开发者

Should the "sub"-activities in a TabActivity bind services?

I am building an application based on a tabbed activity. There are about 12 tabs at any given time.

I have built a download service, for my own use, which requires binding to it. I've found a few questions about the issue binding Services from tabs, but I haven't found anything discussing the best way to design this.

My paths seem to be:开发者_如何学Python

  1. Bind the download service in the tab's activities using getParent() or getApplicationContext() as the context to bind to.
  2. Bind the download service once in the TabActivity and then expose it via a static method to other Activities making up the tabs.
  3. Redesign the download service so that it does not require binding. (I'm not really sure this is a viable option or buys me much)

I'm basically in a toss up between 1 and 2. It seems like #1 seems to make the activities more independent, but I'm not sure if its going to cause problems having the tab "sub" activities binding the same service 12 times on the tab activitie's context. Similarly, I'm not sure if it is good practice to expose state-dependent objects, like a Service, via a static method to other Activities. I'm concerned it may create a number of race conditions that need to be accounted for depending on when the binding happens and when the tab activities are started.

What seems like the better design?


The main purpose of a service is to run long term tasks in the background, even being able to live longer than the app that started the service. By the description on your downloadservice, it seems like it's only handling short term actions, during the lifetime of your app. Therefore I would recommend creating a singleton DownloadManager class that can manage the caching and handle the downloads using worker threads.


I would design this using Context.startService() instead of binding. In my mind, when you bind, you are tying the lifecycle of the service to the lifecycle of the binding activity. If the lifecycles aren't related, you should instead use startService().

If you use startService, you can simply send out a broadcast whenever a download completes to notify the activities (or to update progress) and when all downloads have completed, you can call Service.stopSelf() to shutdown the service.


You can have single activity for all of 12 tabs and change the content/view based on current tab. You can bind the service to the activity object. I think this approach is relevant as all tabs are dealing with same kind of task. Use something like

public class LifeLine extends TabActivity  implements TabHost.TabContentFactory {
public void onCreate(Bundle savedInstanceState) {
TabHost tabHost = getTabHost(); 
TabHost.TabSpec spec
spec = tabHost.newTabSpec("Day").setIndicator("Day",
                          res.getDrawable(R.drawable.icon))
                      .setContent(this);
tabHost.addTab(spec);
...

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tag) {
            if ("Day".equals(tag)) {/* Change the view as needed.*/
                        view.setDuration(1);               
                    } else { /* Change the view as needed.*/}
                view.invalidate();
                Log.v("life", tag);
            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜