tags:

views:

556

answers:

3

Hello,

i want to know what es 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(), cause in onPause() i will do a unbind to the service, so i dont get a serviceConnectionLeak, if i leave the activity. Now if i press the Home-Button and then switching to the HomeScreen, the Activity will unbind from the service, when i go back to the Activity from the Taskmanager, then onCreate() will not be called and if the Code is trying to access a function from the service i will get a null-pointer exception. If i bind and unbind only in onResume() and onPause() i dont have this problem. Am i right?

A: 

What you say is correct. In most cases you will want to register in OnResume() and unregister in OnPause(). If you use OnCreate() and on Destroy() you will still be registering for updates when you are paused, which is being a bad citizen. If you register in OnCreate() and unregister in OnPause(), when you resume the task the registration will be gone, which is almost certainly not what you want.

RickNotFred
"If you use OnCreate() and on Destroy() you will still be registering for updates when you are paused, which is being a bad citizen." Do you have a source you can cite for this statement?
CommonsWare
That was my understanding but I defer to you. Is this not correct?
RickNotFred
Well, not as a general statement, that I am aware of. There is notion of "registering for updates" vis a vis services as a requirement. Certain services you may specifically want to unbind (and therefore perhaps shut down) when your activity goes off-screen. Others you might not. For example, a service that ties into GPS might want to shut down, but only if the application is off-screen for an extended period, so the user gets less frustrated that it has to re-acquire the GPS fix. Now, there may be other better advice than mine, which is why I was asking if you had a source.
CommonsWare
I should have been more specific. The point I was trying to make is that, whenever possible, I think it's a good rule to clean up as soon as you can to leave more resources for other applications. If you have a legitimate need to hold onto the resource longer (as in your example where re-aquiring the resource is lengthy) it's less cut-and-dry.
RickNotFred
+1  A: 

I was asking myself if it is not a problem putting it into onCreate(), cause in onPause() i will do a unbind to the service, so i dont get a serviceConnectionLeak, if i leave the activity.

I register for services in onCreate() and unregister in onDestroy() and have not had a problem with service connection leaks. For example, I just tested this code, confirming that you can press HOME and get no error, and return to the app and get no error.

If i bind and unbind only in onResume() and onPause() i dont have this problem. Am i right?

Yes, but your service may shut down if nothing else is bound to it. That may be your goal, or that may not be your goal. You may also be better served using the onStop()/onStart() pair, since onPause()/onResume() are used in more cases (e.g., a dialog) that may not be worth the unbinding.

CommonsWare
"You may also be better served using the onStop()/onStart() pair, since onPause()/onResume() are used in more cases (e.g., a dialog) that may not be worth the unbinding." I have a activity where i open a progressdialog inside and onPause() is not called or did i misunderstood you?
Norman C.
Ah, sorry, I misunderstood something in the Android docs. A true dialog (e.g., `ProgressDialog`) will not trigger `onPause()`. A dialog-themed activity, however, probably will.
CommonsWare
+1  A: 

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 activities 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 services, registering receivers, etc) to ensure that they are correctly acquired prior to being needed and released (and not leaked) when no longer needed.

hackbod