Search code examples
androidgoogle-mapsgoogle-maps-api-3android-location

Why to call startLocationUpdates in onResume and stopLocationUpdates in onPause?


The docs regarding receive location updates states that startLocationUpdates() should be called in onResume() and stopLocationUpdates() in onPause(), why not in onStart() and onStop()?

What is the benefit for calling in onResume() and onPause()?


Solution

  • That's an "advice", not mandatory. The benefit is to avoid requesting location updates (and hence consume battery) while you are not using/not need the locations because the activity is in background.

    It depends on your use case, you can do it in onStart/onStop, but if you do not use the locations during the onPause->onStop transitions, it's better to put in onPause.

    If you look at the image below, the onPause is called when activity goes in foreground and then, onStop is paused.

    One small difference about background/not visibile: If you have an activity with a ThemeDialog, then the onPause is called, but not the onStop.

    enter image description here