带 AppCompatActivity 的 ActionBarDrawerToggle 和带片段的工具栏后退按钮

ActionBarDrawerToggle with AppCompatActivity and Toolbar Back button with Fragments

我正在将 ActionBarDrawerToggle 与 NavigationView 一起使用。我的内容是使用片段显示的。

我正在关注 this Whosebug question 让后退按钮按下工作,但控制永远不会流向 onOptionsItemSelected

这是我的 MainActivity.class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        removeFragmentFromBackstack();
        updateToolbarWithHomeButton();
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
        case android.R.id.home:
            // doesn't reach here ever.
            return true;
        case R.id.action_x:
            // do something
            return true;
        case R.id.action_y:
            // do something
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void updateToolbarWithBackButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

private void updateToolbarWithHomeButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
    }
}

如何捕获 <- 单击 Toolbar 的后退按钮?


更新:

感谢@mike,工具栏上的后退箭头按钮现在在我的 MainActivity 代码中的 onOptionsItemSelected 中捕获,如下更新。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        removeFragmentFromBackstack();
        updateToolbarWithHomeButton();
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //TODO: skip back press if fragment backstack count is 0.
            onBackPressed();
            updateToolbarWithHomeButton();
            return true;
        case R.id.action_x:
            // do something
            return true;
        case R.id.action_y:
            // do something
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void updateToolbarWithBackButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(false);
    }
}

private void updateToolbarWithHomeButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
    }
}

如果您希望 onOptionsItemSelected() 方法在单击切换时触发,请从 ActionBarDrawerToggle 构造函数调用中删除 toolbar 参数。

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);

否则,切换句柄在内部打开和关闭抽屉,不需要调用 ActionBarDrawerToggle#onOptionsItemSelected()

如果您想根据当前状态以不同的方式处理单击主页 Button,您还需要删除 returns 顶部的 ifonOptionsItemSelected() 方法。

而且,您应该在 onCreate() 中调用 setDisplayHomeAsUpEnabled(true) 一次。您无需一直打开和关闭它。启用和禁用抽屉指示器将解决这个问题。

删除这一行

 if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

来自 onOptionsItemSelected() 所以它看起来像这样

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        // doesn't reach here ever.
        return true;
    case R.id.action_x:
        // do something
        return true;
    case R.id.action_y:
        // do something
        return true;
    default:
        return super.onOptionsItemSelected(item);
}
}