how to override action bar back button in android?

前端 未结 10 711
孤街浪徒
孤街浪徒 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 04:18
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        //noinspection SimplifiableIfStatement
        if (id == R.id.signIn) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    ///////////////////
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
    
    0 讨论(0)
  • 2020-11-30 04:18

    There are several ways how to set up back button in bar:

    1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home

    2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.

    .. for those who wonder why it doesn't work for them...

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

    I have achieved this, by using simply two steps,

    Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"

    example :

     <activity
        android:name=".home.ActivityDetail"
        android:parentActivityName=".home.HomeActivity"
        android:screenOrientation="portrait" />
    

    Step 2: in ActivityDetail add your action for previous page/activity

    example :

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

    Two things to keep in mind that the user can either press back button or press the actionbar home button.
    So, if you want to redirect him to the same destination then you can do this.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return false;
    }
    
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
        startActivity(intent);
        finish();
    }
    

    This will take the user to the intent pressing either key or the action bar button.

    0 讨论(0)
提交回复
热议问题