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?
Asked
Active
Viewed 1,126 times
1
-
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 Answers
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
-
Thanks.. Do you have an example of this implementation using Java? – Heriberto Ureña Aug 30 '18 at 19:01
-
No, I don't, but I guess it shouldn't be difficult to rewrite that code in Java. – theme_an Aug 31 '18 at 07:42