多个/嵌套NavHostFragments - Jetpack导航

3zwjbxry  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(279)

我有两个NavHostFragment,一个在activity(startDestination)中,另一个在Fragment中。我似乎无法在具有NavHostFragment的Fragment中使底部导航工作。
nav_graph.xml (The first nav_graph) (MainActivity)
nav_graph_main.xml (The second navgraph) (Fragment)

NavigationFragment.java(包含第二个导航主机片段的代码)

public class NavigationFragment extends Fragment {
    FragmentNavigationBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_navigation, container, false);
        View view = binding.getRoot();

        NavController navController = Navigation.findNavController(getActivity(), R.id.fragment_navigation_navHostFragment);
        NavigationUI.setupWithNavController(binding.fragmentNavigationBtmNav, navController);

        return view;
    }
}

片段_导航.xml

<fragment
        android:id="@+id/fragment_navigation_navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="false"
        app:layout_constraintBottom_toTopOf="@id/fragment_navigation_btmNav"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph_main"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/fragment_navigation_btmNav"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:menu="@menu/bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

底部_导航. xml(菜单)

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/ic_home"
        android:title="Home" />

    <item
        android:id="@+id/notificationFragment"
        android:icon="@drawable/ic_notification"
        android:title="Notification" />

    <item
        android:id="@+id/waterLevelFragment"
        android:icon="@drawable/ic_water_level"
        android:title="Water Level Map" />

</menu>

Main Activity具有第一个NavHostFragment。最终,用户将能够到达NavigationFragment。导航到NavigationFragment后,NavigationFragment中将有第二个具有BottomNavigationView的NavHostFragment。我希望它能够正常工作。到目前为止,我只看到Activity中BottomNavigation的示例,而没有看到Fragment。
当NavigationFragment.java我导航到www.example.com时,它会抛出一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pup.ccis.mmReady, PID: 20253
java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
    at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:371)
    at androidx.navigation.Navigation.findNavController(Navigation.java:58)
    at com.pup.ccis.mmReady.view.main.NavigationFragment.onCreateView(NavigationFragment.java:39)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我想这是因为它得到的是Activity而不是Fragment。我不知道有什么方法可以把Fragment传递给findNavController方法。
所以基本上我有这样的东西:

--MainActivity (contains 1st NavHostFragment for nav_graph.xml)
|--LoginFragment
|--RegisterFragment
|--MainFragment
|--NavigationFragment (contains 2nd NavHostFragment for nav_graph_main.xml)
  |--HomeFragment
  |--NotificationFragment
  |--WaterLevelFragment
dhxwm5r4

dhxwm5r41#

我很确定我在嵌套的NavHostFragment的场景中遇到了同样的问题。结果是我似乎在片段生命周期中过早地查询了导航控制器。它会找到顶层的而不是当前片段中的那个。我通过将使用导航控制器设置底层导航从onViewCreated移动到onActivityCreated回调来解决这个问题(尽管后者现在已被弃用)

相关问题