Android: Go back to previous activity

前端 未结 23 1576
抹茶落季
抹茶落季 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

    0 讨论(0)
  • 2020-11-22 07:41

    Add this in your onCLick() method, it will go back to your previous activity

    finish();

    or You can use this. It worked perfectly for me

     @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
    
          if ( id == android.R.id.home ) {
             finish();
             return true;
           }
    
      return super.onOptionsItemSelected(item);
      }
    
    0 讨论(0)
  • 2020-11-22 07:42

    If you have setup correctly the AndroidManifest.xml file with activity parent, you can use :

    NavUtils.navigateUpFromSameTask(this);
    

    Where this is your child activity.

    0 讨论(0)
  • 2020-11-22 07:43

    You can explicitly call onBackPressed is the easiest way
    Refer Go back to previous activity for details

    0 讨论(0)
  • 2020-11-22 07:45

    Just this

    super.onBackPressed();
    
    0 讨论(0)
提交回复
热议问题