There are 4 fragments in my application and 3 icons in the bottom navigation bar. When I press a fragment that is not in the bottom navigation bar, the navigation structure breaks down.
First, when I click on one of the food photos on the home page, I switch to the detail fragment. Then I switch to the basket fragment with the button. I cannot switch to the home page fragment from here. When I click on the home page icon in the bottom navigation bar, I switch to the previously open detail fragment. To solve this, I handled all item clicks in the activity, but it still wasn't solved.
detail fragment and can't navigate home fragment
Main Activity
@AndroidEntryPoint
class FoodOrderingActivity : AppCompatActivity() {
private lateinit var binding: ActivityFoodOrderingBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityFoodOrderingBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.food_ordering_fragment_container) as NavHostFragment
NavigationUI.setupWithNavController(binding.bottomNavigationView, navHostFragment.navController)
binding.bottomNavigationView.setOnItemReselectedListener{
when(it.itemId) {
R.id.foodListFragment -> navHostFragment.findNavController().navigate(R.id.foodListFragment)
//R.id.foodDetailFragment -> navHostFragment.findNavController().navigate(R.id.foodDetailFragment)
R.id.basketFragment -> navHostFragment.findNavController().navigate(R.id.basketFragment)
R.id.favoriteFoodFragment -> navHostFragment.findNavController().navigate(R.id.favoriteFoodFragment)
}
}
}
}
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/food_ordering_activity_nav_graph"
app:startDestination="@id/foodListFragment">
<fragment
android:id="@+id/foodListFragment"
android:name="com.omersungur.foodorderingapp.ui.fragment.food_list.FoodListFragment"
android:label="fragment_food_list"
tools:layout="@layout/fragment_food_list" >
<action
android:id="@+id/action_foodListFragment_to_foodDetailFragment"
app:destination="@id/foodDetailFragment" />
<action
android:id="@+id/action_foodListFragment_to_basketFragment"
app:destination="@id/basketFragment" />
</fragment>
<fragment
android:id="@+id/foodDetailFragment"
android:name="com.omersungur.foodorderingapp.ui.fragment.food_detail.FoodDetailFragment"
android:label="fragment_food_detail"
tools:layout="@layout/fragment_food_detail" >
<action
android:id="@+id/action_foodDetailFragment_to_basketFragment"
app:destination="@id/basketFragment"
app:popUpTo="@id/foodDetailFragment"
app:popUpToInclusive="true" />
<argument
android:name="food"
app:argType="com.omersungur.foodorderingapp.model.food.Food" />
</fragment>
<fragment
android:id="@+id/basketFragment"
android:name="com.omersungur.foodorderingapp.ui.fragment.basket.BasketFragment"
android:label="fragment_basket"
tools:layout="@layout/fragment_basket" >
<action
android:id="@+id/action_basketFragment_to_foodListFragment"
app:destination="@id/foodListFragment"
app:popUpTo="@id/foodListFragment" />
</fragment>
<fragment
android:id="@+id/favoriteFoodFragment"
android:name="com.omersungur.foodorderingapp.ui.fragment.favorite.FavoriteFoodFragment"
android:label="fragment_favorite_food"
tools:layout="@layout/fragment_favorite_food" />
</navigation>
bottom nav menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/foodListFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/favoriteFoodFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_favorite"
android:title="Favorite" />
<item
android:id="@+id/basketFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_basket"
android:title="Basket" />
</menu>
How I can fix that? Is it about backstack?
I found the solution to the problem. I used
setOnItem**ReselectedListener
I should have usedsetOnItem**SelectedListener.