how to override action bar back button in android?

前端 未结 10 698
孤街浪徒
孤街浪徒 2020-11-30 03:40

I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator

相关标签:
10条回答
  • 2020-11-30 03:56

    Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      ...
      if (item.getItemId() == android.R.id.home) {
        ....
      }
      ....
    }
    

    but this failed to catch the "Back" button press.

    Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)

    @Override
    public boolean onSupportNavigateUp(){  
      finish();  
      return true;  
    }
    

    and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.

    0 讨论(0)
  • 2020-11-30 04:02

    If you want ActionBar back button behave same way as hardware back button:

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                if (item.getItemId() == android.R.id.home) {
                        onBackPressed();
                        return true;
                }
                return false;
        }
    
    0 讨论(0)
  • 2020-11-30 04:05
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                    switch (item.getItemId()) {
                    case android.R.id.home:
                           finish();
                       break;
                    }
                    return true;
            }
    
    0 讨论(0)
  • 2020-11-30 04:05

    (1) Add Parent activity for your child activity (AndroidManifest.xml)

    <activity
            android:name=".ParentActivity" />
    

    (2) override the onSupportNavigateUp method inside the child activity

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return false;
    }
    
    0 讨论(0)
  • 2020-11-30 04:09

    If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public Intent getParentActivityIntent() {
          return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }
    

    EDIT:
    Also you can achieve the same result by
    setting the launchMode of your parent activity to singleTop.
    So setandroid:launchMode="singleTop" to parent activity in your manifest.
    Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
    reference: Providing Up Navigation

    0 讨论(0)
  • 2020-11-30 04:11

    I think you want to override the click operation of home button. You can override this functionality like this in your activity.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show(); 
            break;
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题