When memory profiling my app, I've noticed that multiple instances of
the same local service class are getting instantiated.  This is not my
expectation from my interpretation of the APIs and service example
code in the SDKs.

My app is designed such that the service represents a data layer,
handling all databases, networking, and some potential background
operations when the Activity is not in the foreground.  The Activity
is responsible for starting the service by calling bindService(..)
during onResume(..).
   context.bindService( new Intent( context,  MyService.class),
service_connection, Context.BIND_AUTO_CREATE );

To allow the memory allocations of the UI/Activity while the Service
continues (for a limited amount of time), the Activity call
unBindService(..):
   context.unbindService( service_connection );
To be clear, the Service does not call selfStop() right away.  Instead
it notes the time of the last unbind and effectively sets a timer.
This part of the code is working just fine.

Now, when I go in and out of the application, I find I'm binding to a
new instance every time I resume the activity.  I assumed I would bind
to the same Service object each time as long the Service did not stop
itself or the process was shutdown for memory reasons.  However, my
logs and the memory profiler do not show this to be the case.

How can I guarantee my process only has one instance of my Service
class instanciated, and that the Activity will rebind to existing
Service upon Resume?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to