KoirN
KoirN

Reputation: 338

Best practice for communication with service in android

What is better and right, use singlenton object that bind to the service on start app, or binding to service and unbinding from service in every activitiy, that use it?

Upvotes: 3

Views: 499

Answers (1)

mach
mach

Reputation: 8395

To bind to a service you need a context. If you implement a singleton object and base its context on an activity you will have trouble as the activity may not live for the duration that you access the service.

If you take the context from the application you will run into the trouble of establishing when to disconnect from the service and may get memory leaks / unnecessary use of memory.

I would recommend binding to the service per activity but use a inheritence-scheme to only have to write the code once.

Best of luck!

Upvotes: 1

Related Questions