Search code examples
androidandroid-fragmentsandroid-tabsandroid-maps

As 'off' to programmatically location on a Activity with fragment, tabs and map


In my application MainActivity, I have two tabs. In tab-1 have a fragment with a RecyclerView, and tab-2 other fragment with a map with some markers. I noticed that regardless of the active tab, the location of my device is active.

In order to save battery life, I wonder if it is possible to 'turn off' location when tab-1 is active, and turn on the location only when the tab-2 is active.

What do you think ?


Solution

  • Yes it is. Just be aware that if you're using GPS, it will take a little while to re-establish position once you start it up again (unless another app is also using GPS). To do so you just call removeUpdates when you're tabbed out and re-request when tabbed in again.

    Whether its a big enough savings to be worth it depends on how long they're on the other tabs. If its brief, it isn't worth it. The one time you absolutely should do so is when your activity is stopped, so you don't request locations when backgrounded.