Reputation: 921
I've a simple service that several of my activities need to access. The standard approach seems to be:
But that requires calling bindService at least once per activity. What if I call bindService exactly once, store the service object globally to allow for different activities to access it?
Is this bad practice? Why?
Upvotes: 2
Views: 779
Reputation: 31503
You could extend Application and let it keep your reference to the service, then in your activity you cast getApplicationContext() to your new class and use a public API to get the service.
mService = ((MyApplication)getApplicationContext()).getMyService();
Upvotes: 1