android的navigation大部分情况是在xml进行配置,但是这样做不灵活,也可以采用在代码里面灵活跳转的方式
主要是通过NavGraph的setStartDestination方法。
-
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.main_view_nav_host_fragment);
-
NavGraph navSimple = navHostFragment.getNavController().getNavInflater().inflate(R.navigation.main_navigation);
-
if (getIntent().getBooleanExtra("jumpfromsettingactivity",false)){
-
if (getIntent().getIntExtra("fragmentnum", 1) == 2) {
-
navSimple.setStartDestination(R.id.connectionFailedWifiFragment);
-
}
-
}
-
else {
-
navSimple.setStartDestination(R.id.realTimeFragment);
-
}
-
navHostFragment.getNavController().setGraph(navSimple);
同时注意要去掉xml里面的app:navGraph
app:navGraph="@navigation/main_navigation"
和默认的startDestination
app:startDestination="@id/XXXFragment"
转载:https://blog.csdn.net/youngboo/article/details/106476686
查看评论