-1

I have an application which play music. I can remotely add music to a device.

I use a service to be able to run it even if myapplication is not started.

I have an custom adapter to display musics in my activity, but musics data are in my service class.

How can get the data from the service and use it to my custom adapter to display musics ?

I found some solutions like to put my objects in a derived Application class, but if all my activities are destroyed I think the application is destroyed too, isn't it?

Thanks

sexyslippers69
  • 334
  • 2
  • 9

1 Answers1

1

Use your suggested solution of putting the objects in a derived Application class.

Your Application will not get destroyed if you still have a Service running. That is the beauty of using this technique - as long as any Activity or Service is running in your app, the Application lives in memory.

Alternatively, a functionally similar technique is to use an explicit singleton for your data. For a long discussion on why this is better than using the application, check out the answers to this question:


The flip side of these techniques is memory management. Since the Application or singleton is always in memory, you may find you are using too much memory with all your objects. So that is something you will have to pay careful attention to.

Consider using weak references to your objects, backed by your DB or shared preferences. Read up a bit about WeakHashMap.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255