Android: Go back to previous activity

前端 未结 23 1549
抹茶落季
抹茶落季 2020-11-22 06:52

I want to do something simple on android app. How is it possible to go back to a previous activity.

What code do I need to go back to previous activity

23条回答
  •  终归单人心
    2020-11-22 07:40

    Are you wanting to take control of the back button behavior? You can override the back button (to go to a specific activity) via one of two methods.

    For Android 1.6 and below:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.
            return true;
        }
    
        return super.onKeyDown(keyCode, event);
    }
    

    Or if you are only supporting Android 2.0 or greater:

    @Override
    public void onBackPressed() {
        // do something on back.
        return;
    }
    

    For more details: http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

提交回复
热议问题