On Tue, Jan 11, 2011 at 2:31 PM, Anm <[email protected]> wrote:
>
> 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 );

It's not shown explicitly in the examples, but I have had better
results binding to my service in the activity's onCreate method.

>
> 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 );

Never tried anything like this.

> 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.

My reading of the docs suggests that a service started by binding to
it will shut itself down right after it's last bound activity unbinds.
If you want the manual stop self to be required, you should start the
service explicitly before binding to it.

> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

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

Reply via email to