When using the Navigation Drawer the Android devs are recommending that in the ActionBar "only those screens that are represented in the Navigation Drawer should actually have the Navigation Drawer image" and that "all other screens have the traditional up carat."
See here for details: http://youtu.be/F5COhlbpIbY
I'm using one activity to control multiple levels of fragments and can get the Navigation Drawer image to display and function at all levels.
When creating lower level fragments I can call the ActionBarDrawerToggle
setDrawerIndicatorEnabled(false)
to hide the Navigation Drawer image and have the Up caret displayed
LowerLevelFragment lowFrag = new LowerLevelFragment();
//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,
lowFrag, "lowerFrag").addToBackStack(null).commit();
The problem I'm having is when I navigate back to the top level fragments the Up carat still shows instead of the original Navigation Drawer image. Any suggestions on how to "refresh" the ActionBar on the top level fragments to re-display the Navigation Drawer image?
Solution
Tom's suggestion worked for me. Here’s what I did:
MainActivity
This activity controls all fragments in the app.
When preparing new fragments to replace others, I set the DrawerToggle setDrawerIndicatorEnabled(false)
like this:
LowerLevelFragment lowFrag = new LowerLevelFragment();
//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,
lowFrag).addToBackStack(null).commit();
Next, in an override of onBackPressed
, I reverted the above by setting the DrawerToggle to setDrawerIndicatorEnabled(true)
like this:
@Override
public void onBackPressed() {
super.onBackPressed();
// turn on the Navigation Drawer image;
// this is called in the LowerLevelFragments
setDrawerIndicatorEnabled(true)
}
In the LowerLevelFragments
In the fragments I modified onCreate
and onOptionsItemSelected
like this:
In onCreate
added setHasOptionsMenu(true)
to enable configuring the options menu. Also set setDisplayHomeAsUpEnabled(true)
to enable the < in the actionbar:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// needed to indicate that the fragment would
// like to add items to the Options Menu
setHasOptionsMenu(true);
// update the actionbar to show the up carat/affordance
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
Then in onOptionsItemSelected
whenever the < is pressed it calls the onBackPressed()
from the activity to move up one level in the hierarchy and display the Navigation Drawer Image:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
switch (item.getItemId()) {
case android.R.id.home:
//called when the up affordance/carat in actionbar is pressed
getActivity().onBackPressed();
return true;
…
}
.replace(R.id.frag_layout
. If this is one more hierarchy level I would expect that you.add
it to the backstack. – DigressivetheDrawerToggle.setDrawerIndicatorEnabled(false);
inside the fragment? I think it is declared in the Main Activity class file. I cant find a way to reference this. Any hints? – IncunabulasetDisplayOptions()
method within theToolbarWidgetWrapper
(of the internal android.support.v7.internal.widget package) wouldn't recreate the icon when entering the same fragment a second time. Just leaving this here for when others stumble upon this problem as well. – Antemeridian