开发者

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜