Jetpack 条件导航

Jetpack conditional navigation

我想根据条件更改我的开始导航片段。我的起始片段可能是片段一或片段二。有实现的方法吗?

我还在答案下方添加了评论以使其更清晰。

// 这里也添加了Java代码

刚刚将 Kotlin 代码转换为 Java

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.home_nav_fragment);  // Hostfragment
NavInflater inflater = navHostFragment.getNavController().getNavInflater();
NavGraph graph = inflater.inflate(R.navigation.nav_main);
graph.setDefaultArguments(getIntent().getExtras());
graph.setStartDestination(R.id.fragment1);

navHostFragment.getNavController().setGraph(graph);
navHostFragment.getNavController().getGraph().setDefaultArguments(getIntent().getExtras());

NavigationView navigationView = findViewById(R.id.navigationView);
NavigationUI.setupWithNavController(navigationView, navHostFragment.getNavController());