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?