1

I want to change my start navigation fragment based on a condition. My start fragment could be fragment one or two. Is there a way to implement it?

enter image description here

random
  • 10,238
  • 8
  • 57
  • 101
  • Currently, there is no support right of the library to determine condition. You should do it within the custom NavFragment which determines the `startDestination` – HawkPriest Aug 29 '18 at 08:12

2 Answers2

1

Check out this question and its solution // Also added Java code here

Just converted Kotlin code to 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());
Maddy
  • 4,525
  • 4
  • 33
  • 53
Akash Patel
  • 3,091
  • 3
  • 15
  • 23
0

Check out this question and it's solution

I have also added a comment below the answer to clear it up a bit.

theme_an
  • 1,506
  • 14
  • 16