简体   繁体   中英

Android Activity Life Cycle Methods

I'm a Newbie to android, I'm Developing a Hangman Application in android my application works fine but only confusion I'm facing is whenever back button is triggered game should not be exited.

I have provided an Alert dialog in the on Pause method to the user weather to exit or resume the game if back button is pressed.But its not working can anyone please say me how to write the Activity cycle methods while developing the games.

First, you shouldn't be adding any dialogs to the back key or disable its behavior. That would just irritate your users.

If you still insist, override onBackPressed() in your activity and don't call super.onBackPressed() there. Call finish() in case you want to exit from the activity.

Required further reading: http://developer.android.com/design/patterns/navigation.html

You have to catch when the back button is pressed. Use this-

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    switch(keycode) 
    {
         case KeyEvent.KEYCODE_BACK:
                     //Write your code here
    }
}

But I don't think it will work in Virtual buttons like in Nexus 5 though, But give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM