以编程方式导航后,带有 NavigationComponent 的 BottomNavigationView 不起作用

BottomNavigationView with NavigationComponent does not work after navigating programmatically

提问人:Kozmotronik 提问时间:11/10/2023 最后编辑:Kozmotronik 更新时间:11/17/2023 访问量:19

问:

我从我开发的一个正在进行的 Android 项目中创建了一个最小的可重现示例。它有 3 个屏幕:主页仪表板通知,可通过 .问题是当我尝试在按下按钮后从主页导航到 Dahsboard 页面时,主页中的图标不再响应。BottomNavigationViewBattomNavigationView

下面是一个 gif,显示了我试图解释的内容:
Problem illustration gif

观看原始视频

我希望它能在我点击它时导航回主页。但它不会向后导航。只有当我单击Android导航栏的后退按钮时,它才会发生。我在这里错过了什么?谢谢。

我使用 Android Studio 的 Bottom Navigation Views Activity 创建了该示例。这是我以编程方式导航的代码:

public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel homeViewModel =
            new ViewModelProvider(this).get(HomeViewModel.class);

    binding = FragmentHomeBinding.inflate(inflater, container, false);
    View root = binding.getRoot();
    
    final Button button = binding.button;
    button.setOnClickListener(v -> {
        NavController navController = NavHostFragment.findNavController(this);
        navController.navigate(R.id.navigation_dashboard);
    });
    return root;
}

这是我尝试的另一种方法,但无济于事,结果相同:

binding.button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.navigation_dashboard));
java android bottomnavigationview

评论


答: 暂无答案