'Back button in action bar opening and closing NavDrawer
I have a nav drawer set up in my main activity. In one of my fragments I want to hide the hamburger icon and show the back arrow (achieved)
ActionBar bar = mMainActivity.getSupportActionBar();
bar.setDisplayShowHomeEnabled(true);
bar.setDisplayHomeAsUpEnabled(true);
bar.setDefaultDisplayHomeAsUpEnabled(true);
The problem is if I click on the arrow it opens the drawer. So I added this: mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
The problem is when I add this line, nothing displays. The drawer icon and the back or up icon. So can anyone tell me what I'm doing wrong.?
EDIT
There seems to be confusion as to what I have done and not done. The mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
was added temporarily, I removed it again as when I did, there were no icons displayed neither nav drawer or back arrow.
Below is how I handle my menu item clicks:
@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.
switch (item.getItemId()){
case R.id.search_badge:
L.i("search", "search");
return false;
case android.R.id.home:
L.i("home", "home pressed");
break;
case R.id.action_done:
actionDone();
//for testing
//checkSelectedInterests();
//TODO: MLC
L.i("done", "done pressed");
break;
default:
;;
}
return super.onOptionsItemSelected(item);
}
however
case android.R.id.home:
L.i("home", "home pressed");
break;
is never hit
I hope this clarifies it. So at the moment the nav drawer icon is hiding and the back arrow is displayed. When I click the back arrow it opens the nav drawer instead of entering my switch case.
Solution 1:[1]
So there are 2 parts to your question:
- Displaying the home button or back button:
Only to show home button:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Drawable homeMenu = getResources().getDrawable(R.drawable.ic_menu_white_24dp);
getSupportActionBar().setHomeAsUpIndicator(homeMenu);
getSupportActionBar().setHomeButtonEnabled(true);
To show a back arrow instead of home:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.fab_material_white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
This only changes the image displayed.
To change the behaviour of the hamburger icon. In the activity where you want to change the behaviour of the home button, change the onclick of the menu item as shown to
onBackPressed()
:public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; default: return super.onOptionsItemSelected(item); } }
Leave it as it is for the normal navigation drawer. Hope this helps. This is how I have done for my app.
EDIT:
Remove the mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
and then proceed with the 2nd part of the answer to do a onBackPressed()
instead of the default hamburger functionality.
Solution 2:[2]
Perhaps it's related but not quite to this question, but it could be helpful for somebody.
If you're using NavigationComponent
and your startDestination
fragment isn't (or shouldn't be) presented in the NavigationView
menu then when you setting up your AppBarConfiguration
you need to add id
of your startDestination
from your navigation graph:
appBarConfiguration = AppBarConfiguration(setOf(R.id.homeFragment, R.id.menu_1, R.id.menu_2, R.id.menu_3), drawerLayout)
Solution 3:[3]
Sounds like you set something up incorrectly. Check out this tutorial, it may help you figure out what you're missing.
EDIT This is literally the first sentence in the link. For the up
button to work, you need to set up parent activity in the manifest.
To implement Up navigation, the first step is to declare which activity is the appropriate parent for each activity. Doing so allows the system to facilitate navigation patterns such as Up because the system can determine the logical parent activity from the manifest file.
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | DropDrage |
Solution 3 |