通过 NavigationComponent 在 BottomNavigation 片段之间传递数据

Passing Data between BottomNavigation fragments via NavigationComponent

是否可以通过 NavigationContoller 在 BottomNavigation 控制的 Fragment 之间传递 Parcelable object/arguments?

这是我的应用程序的流程,用户登录到应用程序并打开一个包含 BottomNavigation 的片段。我能够获得第一个片段的参数(第一个片段使用 NavHostFragment.findNavController(this).navigate(action) 打开)我能够向该操作添加参数并传递值,但 BottomNavigation 没有任何导航指令导航是通过 menuID 完成的。我如何通过捆绑在所有这些底部导航片段之间传递登录用户参数。

我尝试使用 onDestinationChanged() 传递参数

@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        NavArgument argument = new NavArgument.Builder().setDefaultValue(selectedUser).build();
        destination.addArgument("user", argument);
}

但应用程序仍然崩溃 java.lang.IllegalArgumentException: Required argument "user" is missing and does not have an android:defaultValue

你可以使用SharedViewModel also in navigation docs Pass data between destinations他们推荐

In general, you should strongly prefer passing only the minimal amount of data between destinations. For example, you should pass a key to retrieve an object rather than passing the object itself, as the total space for all saved states is limited on Android. If you need to pass large amounts of data, consider using a ViewModel as described in Share data between fragments.

我们可以像 asad 提到的那样使用 SharedViewModel,或者我们可以使用 NavGraph 来设置参数

navController.getGraph().findNode(R.id.todoFragment)
            .addArgument("user", new NavArgument.Builder()
                    .setDefaultValue(user)
                    .build());
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    navController = Navigation.findNavController(getActivity(), R.id.navBottomNavigation);
    NavigationUI.setupWithNavController(R.id.bottomNavigationView, navController);
    NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.nav_sub_graph);

    NavArgument argument = new NavArgument.Builder().setDefaultValue(yourObject).build();
    navGraph.addArgument("yourObject",argument);

    navController.setGraph(navGraph);
}
在 BottomNavigation 中传递对象嵌套片段

 navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {

            switch (destination.getId()) {
                case R.id.profileFragment:
                    NavArgument argumentProfile = new NavArgument.Builder().setDefaultValue(yourObject).build();
                    destination.addArgument("yourObject",argumentProfile);

                case R.id.homeFragment:
                    NavArgument argumentHome = new NavArgument.Builder().setDefaultValue(yourObject).build();
                    destination.addArgument("yourObject",argumentHome);

                case R.id.orderFragment:
                    NavArgument argumentOrder = new NavArgument.Builder().setDefaultValue(yourObject).build();
                    destination.addArgument("yourObject",argumentOrder);
            }
        }
    });