Original: Is there a way to kill a child activity of a TabActivity. I have a very simple tab setup. There are two tabs. One of the tabs contains a MapActivity. When I switch to the MapActivity the GPS turns on. When I press back, the TabActivity exits, but the GPS is still on. The gps doesn't turn off until I explicitly kill the app. Is there a way to kill the MapActivity from the TabActivity? I would like to kill it when TabActivity exits/onBackPressed.
Edit:
I do attempt to stop location requests in onDestroy and onPause in my MapActivity. I am using the MyLocationOverlay class an calling disableCompass and disableMyLocation in the onDestroy method. These methods are called, but the GPS remains on. If I call enableMyLocation and immediately call disableMyLocation (during onCreate), the gps will turn off.Edited title to reflect the ans开发者_StackOverflowwer: Turns out the problem is unexpected behavior with MyLocationOverlay's enable*/disable*. These functions do not appear to be idempotent. Multiple calls to enable and disable do not turn it on once or off once as one might expect. Ensuring one call to enable (in onResume) and one call to disable (in onPause) will disable location requests when exiting an activity.
The location service cannot be killed by stopping an activity, you should read up more on location services and how they operate:
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Specifically, when you request location updates from the providers (in your case the GPS provider) you should only request it for a certain period of time, and then you need to make a call to stop the requests.
Also see: http://developer.android.com/reference/android/location/LocationManager.html
The best thing to do would be to override onStop() or onBackPressed() to stop the GPS. When you are in one of the activities, you have the option of calling getParent() to return the tab activity, as a side note.
It turns out that calling MyLocationOverlay.enable*() multiple times (I was calling it twice) makes the GPS stay on even if you call MyLocationOverlay.disable*() as many times. At least, this is the behavior I observed. I expected enable and disable to be idempotent, but they are not. Related problem
精彩评论