4

I am trying to use Navigation and SafeArgs components in my Android application along with BottomNavigationView but I'm not able to pass arguments through a bottomnavigationview.

This is my case:

  • HomeActivity: hosts all fragments and pass an user-id to all fragments
    • FragmentA: retrieve the user-id and do something..
    • FragmentB: as FragmentA
    • FragmentC: as FragmentA

In my HomeActivity I've setted up the navigation component to switch among fragments:

uid = "hello"
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav?.let {
            NavigationUI.setupWithNavController(it, navController)
        }

and passed the user-id with:

navController.addOnDestinationChangedListener { _, destination, arguments ->
    when(destination.id) {
       R.id.navigation_storyboard -> {
       val argument = NavArgument.Builder().setDefaultValue(uid).build()
       destination.addArgument("uid", argument)
       }
    }
}

Inside FragmentA, I tried to get data either by using FragmentAArgs.fromBundle(arguments!!).uid and the delegate property by NavArg() but I get null (or just default value)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
v2to
  • 409
  • 1
  • 5
  • 11

0 Answers0