我无法在底部导航栏中正确导航 - android 底部导航视图

问题描述 投票:0回答:1

我的应用程序中有 4 个片段,底部导航栏中有 3 个图标。当我按下不在底部导航栏中的片段时,导航结构就会崩溃。

首先,当我点击主页上的一张食物照片时,我切换到细节片段。然后我用按钮切换到篮子片段。我无法从这里切换到主页片段。当我单击底部导航栏中的主页图标时,我切换到之前打开的详细信息片段。为了解决这个问题,我处理了活动中的所有项目点击,但仍然没有解决。

detail fragment and can't navigate home fragment

主要活动

@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)
            }
        }
    }
}

导航图

<?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>

底部导航菜单.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>

我该如何解决这个问题?是关于backstack吗?

android navigation fragment bottomnavigationview android-bottomnav
1个回答
0
投票

您可以使用

addOnDestinationChangedListener

yourNavController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id == R.id.YourFragmentId) {
              // navigation process - navigate  
            } 
       }
© www.soinside.com 2019 - 2024. All rights reserved.