how can I do something before current activity go background by Back key?
When back key is pressed, current activity goes backgr开发者_如何学Pythonound. I'd like to show a popup and let user choose really close this activity, before current activity go background. I tried to override onPause(), but it's called after activity goes back.
Please somebody explains me how to do that?
You can override the onKeyDown method of the Activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//Show the dialog and get the response
}
//Do a if here with your variable returned from the dialog
return super.onKeyDown(keyCode, event);
}
Did you try to override onPause() ?
In my case when I hit the back button it first call onPause() before calling onStop().
There is a good video explaining an application lifecycle processus :
http://blip.tv/file/958450/
精彩评论