I want to use the default Navigation Drawer Activity of Android Studio (v. 3.5). After creating this default activity (New Project --> Navigation Drawer Activity) I started this templet. If I click on one of the icons of the navigation menu (e.g. “Gallery”) the current fragment of the NavHost doesn’t change. As I understand the following section: https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer the NavController should navigate to the chosen destination if the id of the Menu Item matches the id of the destination, but this doesn’t work.
After researching the whole day, I found the following Question on Stackoverflow : NavigationView click event which is very similar but not really answered. The next interesting point is that if I use the Button Navigation Activity this template seems to work probably and I can’t really figure out the differences to the Navigation Drawer Activity templet. I also found several other solutions which worked for older Templets. I can’t understand why there is a standard templet which seems not to work and why there are also no proper examples and tutorials provided via https://developer.android.com/ .
mobile_navigation.xml (navigation graph):
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
... >
...
<fragment
android:id="@+id/nav_tools"
android:name="ktu.workout_planner.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
</navigation>
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
...
<item
android:id="@+id/nav_tools"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
As you can see the id of the item matches the id of the fragment.
onCreate function of MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
Did I miss something why clicking on the item of the Navigation Drawer doesn’t open the corresponding fragments?
Answer by Mike M. (see comment):
There's an issue in 3.5 that's causing XML to be rearranged improperly. In the
activity_main
layout, make sure that the<NavigationView>
is listed last within the<DrawerLayout>
. That is, move it to after the<include>
there, if you still have the default setup. Then, have a look at this post to see how to stop Android Studio from reordering yourView
s like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With