Should sub-activities in TabActivity bind services?

I am creating a tabbed activity based application. About 12 tabs are available at any given time.

I created a download service for my own use, which requires binding to it. I found some questions about Services related issues from the tabs, but I didn't find anything discussing the best way to develop it.

My paths seem like this:

  • Bind the download service to tab actions using getParent () or getApplicationContext () as the context for the binding.
  • Bind the download service once in TabActivity, and then set it through the static method for the other actions that make up the tabs.
  • Redesign of the download service so that it does not require binding. (I'm not sure if this is a viable option or it buys me a lot).

I basically toss between 1 and 2. It seems that # 1 seems to make the actions more independent, but I'm not sure if this will cause problems with the sub tabs that link the same service 12 times in the context of the tab activity. Similarly, I am not sure that it is good practice to expose state-dependent objects, such as a Service, using a static method for other activities. I am worried that he may create a number of race conditions that must be taken into account depending on when the snap occurs and when the tabbed actions begin.

What does the best design look like?

+3
source share
3

- , , , . , . SingleMan DownloadManager, .

+1

, Context.startService() . , , . , startService().

startService, , , ( ), , Service.stopSelf() .

0

You can have one action for all 12 tabs and change the content / view based on the current tab. You can bind a service to an activity object. I think this approach matters because all tabs deal with the same 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
source

All Articles