6

I have a service that is being used/bound in multiple activities (I carefully wrote it so that one activity will unbind it before another binds, in onPause/onResume). However, I noticed a member in the Service won't stick....

Activity 1:

private void bindService() {
    // Bind to QueueService
    Intent queueIntent = new Intent(this, QueueService.class);
    bindService(queueIntent, mConnection, Context.BIND_AUTO_CREATE);
}

...

bindService();

...

mService.addItems(downloads);     // the initial test adds 16 of them

Activity 2:

bindService();                             // a different one than activity 1
int dlSize = mService.getQueue().size();   // always returns 0 (wrong)

Service's code:

public class QueueService extends Service {
    private ArrayList<DownloadItem> downloadItems = new ArrayList<DownloadItem();

    // omitted binders, constructor, etc

    public ArrayList<DownloadItem> addItems(ArrayList<DownloadItem> itemsToAdd) {
        downloadItems.addAll(itemsToAdd);
        return downloadItems;
    }

    public ArrayList<DownloadItem> getQueue() {
        return downloadItems;
    }
}

Upon changing one thing -- making the service's downloadItems variable into a static one -- everything works perfectly. But having to do that worries me; I've never used a singleton in this way before. Is this the correct method of using one of these?

  • 1
    Do you call startService() anywhere in your activities? This allows the service to stay alive as a singelton. Otherwise it will be destroyed when the activity which bound to it is destroyed. – Nospherus Apr 30 '13 at 22:19
  • @Nospherus I'll add what I did shortly -- tl;dr does "bindService" work just as well as "startService()"? –  Apr 30 '13 at 22:51
  • 3
    No. You have to call both startService() and bindService(). If you only call bindService(), then the service will die as soon as you unbind it. By calling startService(), it will stay alive until you call stopService()(or stopSelf() inside the service). – Nospherus Apr 30 '13 at 22:55
  • @Nospherus thanks; that's exactly what I needed to know! I will, or would, choose you as best answer. –  Apr 30 '13 at 22:57

2 Answers2

7

It turns out Nospherus was correct; all I needed to do was andd a startService() call beside my bindService() one, and all was well.

Because multiple startService() calls don't call the constructor multiple times, they were exactly what I needed. (This is extremely lazy on my part, but it works for now. I am unsure of how to check for a started (and not bound) service.) My code now looks like this:

Intent queueIntent = new Intent(getApplicationContext(), QueueService.class);
bindService(queueIntent, mConnection, Context.BIND_AUTO_CREATE);
startService(queueIntent);

See also Bind service to activity in Android

Community
  • 1
  • 1
0

Service is always Singleton by default

Only one instance of service can ever exist at a given time. If a Service is running then there is no way you can create another instance of the Service. Period.

Binding with multiple Activity

You can bind a service to n number of Activities. Each binding works independently. When you move from one Activity to another then the changes done by Activity1 will persist when you move to Activity2 only if the service is alive.

enter image description here

Then why the changes don't persist in my case?
To understand this we should know the lifespan of a service

Lifespan of a Service

A service exist between onCreate() and onDestroy()

Case 1:
enter image description here Case 2: enter image description here

Community
  • 1
  • 1
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88