Search code examples
androidgpsback-buttononpauseandroid-homebutton

onPause(), different results from home and back button


I'm creating an application which uses locationManager() to handle updates from the GPS. When the app is closed, I want the updates from the locationManager to stop and has this piece of code at the moment.

@Override
protected void onPause() {
    super.onPause();
    if(locationManager != null){
        locationManager.removeUpdates(locationListener);
}

When I exit the app with the back button, onPause() is called and the GPS stop. Nothing weird here. But, if I press the home button, the GPS marker in the status bar remains visible, even though onPause is called just the same.


Solution

  • That is because when you exit an app with back button Activity is destroyed (receives onDestroy callback). And as there is no process uses GPS marker is also hidden. If you lock device screen or press home button application is still run but activity is stopped. With your code you won't get any GPS updates after onPause until you register for them again.