I want to know what is the best place in an Activity to bind to a service?
I saw examples doing it in onResume()
and also in onCreate()
. I was asking myself if it is not a problem putting it into onCreate()
, because in onPause()
I will do a unbind to the service, so I don't get a serviceConnectionLeak, if I leave the activity. Now if I press the Home Button and then switch to the Home Screen, the Activity will unbind from the service, when I go back to the Activity from the Task Manager, then onCreate()
will not be called and if the code is trying to access a function from the service I will get a NullPointerException. If I bind and unbind only in onResume()
and onPause()
I don't have this problem. Am i right?
onResume() will never be called before onCreate() . Save this answer. Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
onStart() -> called when the activity becomes visible, but might not be in the foreground (e.g. an AlertFragment is on top or any other possible use case). onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Save this answer.
Binding to a service. Application components (clients) can bind to a service by calling bindService() . The Android system then calls the service's onBind() method, which returns an IBinder for interacting with the service.
I would generally recommend doing this in either onCreate()
/onDestroy()
or onStart()
/onStop()
, depending on the semantics that you want:
If your Activity
wants to be interacting with the Service
the entire time it is running (for example maybe it can retrieve some data from a network for you and will return the data when ready and you want to allow this to happen while in the background so if the user returns you will have the data ready), then onCreate()
/onDestroy()
is probably appropriate. Note that the semantics here is that the entire time your Activity
is running it needs the Service
, so if this Service
is running in another process then you have increased the weight of it and made it more likely for it to be killed while in the background.
If your Activity
is only interested in working with the Service
while visible, then onStart()
/onStop()
is appropriate. This means your Activity
will unbind from the Service
when the user leaves it (and it is no longer visible) and connect back up the next time the return and it is re-started and resumed.
I generally wouldn't recommend doing bind/unbind in onResume()
and onPause()
. These generally won't decrease significantly the amount you use the Service
(and thus your overhead), and in fact, because a pause and resume happens at every activity transition, this is a code path you want to keep as lightweight as possible. Doing it here can have other unexpected negative consequences: for example if multiple Activity
s in your app bind to the same Service
, when there is a transition between two of those activities the Service
may also get destroyed and recreated as the current Activity
is paused before the next one is resumed.
Also these pairs (onCreate()
/onDestroy()
, onStart()
/onStop()
, onPause()
/onResume()
) are intended to be the proper pairs for acquiring and then releasing resources (such as binding to Service
s, registering receivers, etc) to ensure that they are correctly acquired prior to being needed and released (and not leaked) when no longer needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With