Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating from navigational drawer using Android Jetpack

I am trying to implement jetpack navigation on my android app. I have created a navigational graph as shown below:

a picture of the Graph editor

and navigational drawer menu xml as shown below:

a picture of the menu navigational drawers menu items design

according to the documentation in the android developer site, here

giving the menu items the same id as the fragments or destinations should be enough to ensure that clicking on an menu item will navigate me to the specified fragment. This doesn't seem to work, am I missing something?

like image 363
Dracarys Avatar asked Nov 29 '25 11:11

Dracarys


2 Answers

@Dracarys You should use

implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha04"

instead of

implementation "android.arch.navigation:navigation-ui:1.0.0-alpha04"

pay attention to -ktx part in case if you are using Kotlin. Then you will see navigationView.setupWithNavController(navController) method

[UPD 1]

Also, I faced with same issue, when I get navController by calling findNavController(view). Try to use findNavController(activity, id) and pass it to setupWithNavController(navController) method. It works for me now

like image 176
ndubkov Avatar answered Dec 02 '25 04:12

ndubkov


According to the docs, there's a piece of code wiring you need to do:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navigationView, navController);

or in kotlin:

val navigationView = findViewById(R.id.nav_view)
navigationView.setupWithNavController(navController)
like image 37
Levi Moreira Avatar answered Dec 02 '25 03:12

Levi Moreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!