J'utilise ActionBarDrawerToggle avec NavigationView. Mon contenu est affiché en utilisant des fragments.ActionBarDrawerToggle avec AppCompatActivity et barre d'outils Bouton Précédent avec des fragments
Je suis this stackoverflow question pour que le bouton de retour soit pressé pour fonctionner mais le contrôle ne passe jamais à onOptionsItemSelected
.
Ceci est mon 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();
}
}
Comment puis-je capturer le bouton Précédent <- clic de Toolbar
?
Mise à jour:
Merci à la touche @ Mike flèche de retour sur la barre d'outils est maintenant capturé dans le onOptionsItemSelected
dans mon code MainActivity mis à jour ci-dessous.
@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();
}
}
J'ai retiré la barre d'outils de nouvelle ActionBarDrawerToggle (ce, mDrawerLayout, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes) mais le cas de commutateur pour MenuItem avec id android.R.id.home ne passe pas. Y a-t-il un identifiant différent pour le bouton Retour? –
L'appel à 'mDrawerToggle.onOptionsItemSelected (item)' renvoie 'true' lorsque l'ID' MenuItem' est 'android.R.id.home', de sorte que le bloc' if' retourne avant que l'exécution n'atteigne 'switch' . Vous pouvez simplement déplacer cet appel vers votre premier «cas». –
J'ai ajouté une instruction log dans le bloc 'if' mais cela ne retourne pas vrai pour' mDrawerToggle.onOptionsItemSelected (item) 'quand je clique sur le bouton retour –