1

I'm looking for a way to detect that my android app has been backgrounded, so that regardless of which Activity was running upon pressing the Home key (or return key on the last activity in stack) then a specified function will be called.

onPause() and onStop() cannot distinguish between a new activity replacing the current one and the app being backgrounded, so I'm looking for advice as to what additional test(s) I must include to ascertain this.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KomodoDave
  • 7,239
  • 10
  • 60
  • 92
  • So basically, you want to detect when user leaves your activity for Home screen? – inazaruk Jun 05 '11 at 21:15
  • Can you please shortly describe why do you want this? – inazaruk Jun 05 '11 at 21:54
  • I have a bluetooth connection established after app startup/foregrounding, and maintained while any activity is running. I need to drop the connection if the app is backgrounded. – KomodoDave Jun 05 '11 at 22:01
  • Ok, this is not an android way of doing things. You should consider changing your design. First of all, move your bluetooth connection handling to Service. Then bind to service from your activity. – inazaruk Jun 05 '11 at 22:10
  • That doesn't solve my problem. I would still have to stop the service when my app is backgrounded. – KomodoDave Jun 05 '11 at 22:25
  • I don't think there is documented way to detect when user navigates to Home Screen or not. But consider scenario when user navigates to your activity, then clicks "Home" button for three seconds, "Recent" screen shows up and user navigates to activity in different app. So, basically, user leaves your app without navigating to "Home" screen. – inazaruk Jun 05 '11 at 22:36
  • You can introduce `Service` and then detect the the moment when all activities in your app are inactive for at least several minutes, and then drop bluetooth connection. – inazaruk Jun 05 '11 at 22:40

1 Answers1

2

Using a service, as others have suggested, is the right thing to do.

Broadly, the different Activities that make up your application should all bind to the service in their onResume() method. They should unbind from the service at onPause().

Your service's onUnbind() method will be called when there all the previously connected clients have disconnected, which would be the point where you would drop the Bluetooth connection.

More information about services, and binding to them from activities, at http://developer.android.com/reference/android/app/Service.html.

Nik
  • 2,380
  • 2
  • 16
  • 15
  • From http://developer.android.com/guide/components/bound-services.html: **You should usually not bind and unbind during your activity's onResume() and onPause()**, because these callbacks occur at every lifecycle transition and you should keep the processing that occurs at these transitions to a minimum. Also, if multiple activities in your application bind to the same service and there is a transition between two of those activities, the service may be destroyed and recreated as the current activity unbinds (during pause) before the next one binds (during resume). – whlk Oct 01 '12 at 09:40