AndroidJetpack导航组件——Navigation用于Fragment跳转

1 控件监听函数内跳转

FloatingActionButton fab_add = requireActivity().findViewById(R.id.fab_add);
        fab_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_nav_home_to_selectFragment);
            }
        });
    }

2 Fragment内随时跳转

NavController navController = Navigation.findNavController(getView());
navController.navigate(R.id.action_QRScanFragment_to_modifyFragment);

ps:前提是/res/navigation/xxx.xml内添加了对应Fragment并连好了跳转路径图。

你可能感兴趣的:(Android)