带有AppCompatActivity的ActionBarDrawerToggle和带有片段的工具栏后退按钮 [英] ActionBarDrawerToggle with AppCompatActivity and Toolbar Back button with Fragments
问题描述
我将ActionBarDrawerToggle与NavigationView一起使用.我的内容是用片段显示的.
I am using the ActionBarDrawerToggle with NavigationView. My content is displayed using fragments.
我正在关注此stackoverflow问题以获取后退按钮按下即可工作,但控制永远不会流向onOptionsItemSelected
.
I am following this stackoverflow question to get the back button press to work but control never flows to onOptionsItemSelected
.
这是我的MainActivity.class:
This is my 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
中的后退按钮<-?
How can I capture the Back button <- click from Toolbar
?
现在,感谢@mike,在我的MainActivity代码的onOptionsItemSelected
中捕获了工具栏上的后退箭头按钮,如下所述.
Thanks to @mike the back arrow button on the toolbar is now captured within the onOptionsItemSelected
in my MainActivity code as updated below.
@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
自变量.
If you want the onOptionsItemSelected()
method to fire upon clicking the toggle, remove the toolbar
argument from the ActionBarDrawerToggle
constructor call.
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
否则,切换按钮将在内部处理抽屉的打开和关闭,而不必调用ActionBarDrawerToggle#onOptionsItemSelected()
.
Otherwise, the toggle handles opening and closing the drawer internally, and the call to ActionBarDrawerToggle#onOptionsItemSelected()
isn't necessary.
如果要根据当前状态以不同方式单击主页Button
,则还需要删除在onOptionsItemSelected()
方法顶部返回的if
块.
If you want to handle clicking the home Button
differently depending on the current state, you'll also want to remove the if
block that returns at the top of the onOptionsItemSelected()
method.
而且,您应该仅在onCreate()
中调用一次setDisplayHomeAsUpEnabled(true)
.您无需保持打开和关闭状态.启用和禁用抽屉指示器将解决此问题.
And, you should call setDisplayHomeAsUpEnabled(true)
just once in onCreate()
. You don't need to keep switching that on and off. Enabling and disabling the drawer indicator will take care of that.
这篇关于带有AppCompatActivity的ActionBarDrawerToggle和带有片段的工具栏后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!