Android Detect what caused the onPause
I am not sure if there is a better way of doing this but I want to detect somehow what caused the application to pause.
On one particular activity that displays a tracking map if the user click back or home I want to stop GPS but if the screen goes off I want to keep the gps running I am also using a wake lock so it doesn't sleep (yes I know this probably should be in a service, but that will be v2 I'm running out of time!)
I am overriding when the back button is pressed
@Override
public void onBackPressed() {
wl.开发者_StackOverflowrelease();
this.mMyLocationOverlay.disableMyLocation();
this.mMyLocationOverlay.disableCompass();
if (mLocManager != null) {
mLocManager.removeUpdates(mLocListener);
}
super.onBackPressed();
}
but I can't find a way of doing the same for home.
Can anyone help?
Bex
in the onPause()
you can call isFinishing()
to know if the activity is finishing for whatever may be the reason or simply getting paused. See the doc here
But first please check whether your logic that you have written comes to onBackPressed()
. write some log to confirm that that part of your code is active
If i understand you correctly. You have one activity which has some view from that activity you have started another activity. But you want when you click back key of the latest activity some actions you have to perfrom in previous activity. If this is the case you have to write logic in onResume()
method. Because as soon as you click back key activity on top is finished and your focus comes to onResume()
method of previous activity. donot override onKEyDown()
or onBackPressed()
.
Donot forget to vote if you feel my response is useful for you.
Thanks Deepak
精彩评论