I'm in the middle of building an Android version of my iOS app and could use some guidance as to how to implement certain features.
The app has several responsibilities; monitoring location changes and adding them to a DB, advertising and discovering available peers via Bonjour, maintaining connections to discovered peers and to a server, and advertising and listening for nearby peers via Bluetooth LE.
To organize them, I created 'Manager' singleton classes for each responsibility (e.g. LocationManager, BonjourManager, etc..) I start them when the app starts and stop them when the app terminates.
Obviously, I don't want them running in the background when the user is not interacting with the app. In iOS this was simple enough; each manager registers itself for lifecycle notifications, pausing on appDidEnterBackground
and resuming on appDidEnterForeground
.
My question is: How can I 'manage' these managers in Android so that they stop running when the app in not visible to the user and resume when the app is opened again? As far as I know, there is no global onPause
and onResume
which get called when the app switches between background and foreground.
I looked into using BoundServices but then I would need a binding between every activity and every manager so that the managers aren't destroyed until all activities unbound?
Help! I have a feeling that managers maybe aren't the right way to keep these activities alive...