SinaMN75
SinaMN75

Reputation: 7621

Dynamically change fragment destination in bottom navigation

I'm using bottom navigation with navigation component architecture. I want to change the first fragment destination dynamically. so I have Fragment1 and Fragment2. I want in some conditions, clicking on the first item in bottom nav open the Fragment1 and in other conditions open Fragment2

Upvotes: 3

Views: 856

Answers (2)

Ahmed Saad
Ahmed Saad

Reputation: 175

setOnNavigationItemSelectedListener deprecated

bottomNavigationView.setOnItemSelectedListener{ item ->
    when (item.itemId) {
            R.id.firstItem -> {
                if(condition){
                    navController.navigate(fragment1)
                }else{
                    navController.navigate(fragment2)
                 }
            }
    }
}

Upvotes: 1

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363479

You can use something like:

    bottomNavigationView.setOnNavigationItemSelectedListener {
        if (it.itemId == ..){
            //navigate to Fragment1 or Fragment2
            true
        }
        //Trigger the original listener for the other items
        NavigationUI.onNavDestinationSelected(it, navController)
        true
    }

Upvotes: 4

Related Questions