Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass data between two fragments destination If I tap a menu in navigation controller?

I am trying to use navigation controller. I have a bottom navigation view. that located on my MainActivity, and it is initiated using the code below on :

class MainActivity : AppCompatActivity() {

    lateinit var navController : NavController
    lateinit var logoHeaderImageView : ImageView
    var toolbarMenu : Menu? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        logoHeaderImageView = findViewById(R.id.header_lakuin_image_view)

        navController = Navigation.findNavController(this,R.id.nav_main_host_fragment)



        setupBottomNavMenu(navController)
        setupActionBar(navController)



    }

    fun setupBottomNavMenu(navController: NavController) {
        NavigationUI.setupWithNavController(bottom_navigation_view,navController)
    }

    fun setupActionBar(navController: NavController) {
        setSupportActionBar(main_activity_toolbar)
        main_activity_toolbar.title = ""


        val appBarConfiguration = AppBarConfiguration(
            setOf(
                // set some destination as the top hierarchy destination, to make the up button doesn't show.
                R.id.destination_home,
                R.id.destination_order,
                R.id.destination_favourite,
                R.id.destination_cart,
                R.id.destination_profile

            ))

        NavigationUI.setupWithNavController(main_activity_toolbar,navController,appBarConfiguration)
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main_toolbar, menu)
        toolbarMenu = menu
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
    }




}

here is the look of my bottom navigation view: enter image description here

So I want to pass data from my HomeFragment (destination home) to OderFragment (destination order). I usually using bundle or safeArgs to pass data like the code below:

var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)

but I don't know where to place that code, If I want to pass data from my HomeFragment to OderFragment

like image 374
sarah Avatar asked Dec 13 '22 12:12

sarah


1 Answers

When using NavigationUI.setupWithNavController(bottom_navigation_view,navController) (or, if you're using the navigation-ui-ktx Kotlin extension bottom_navigation_view.setupWithNavController(navController)), you can't pass any custom arguments to destinations - an important part of global navigation is that they always take you to the same screen in the same state.

Generally, you should be holding data like the current amount separately from Navigation arguments - whether it is in a persisted database, SharedPreferences, or some other location that would survive process death, allowing users to continue with what they're doing even after restarting their phone, etc.

However, if you must use Navigation arguments for this, you can set the default argument for your destination ahead of time (i.e., whenever your amount changes):

NavDestination orderDestination = navController.graph.findNode(R.id.destination_order)
orderDestination.addArgument("amount", NavArgument.Builder()
    .setType(NavType.FloatType)
    .setDefaultValue(amount)
    .build())

Afterwards, your BottomNavigationView triggering R.id.destination_order will automatically include that argument, along with your new amount value, by default.

like image 82
ianhanniballake Avatar answered Dec 25 '22 23:12

ianhanniballake