I use NavController + DrawerLayout, set some menu-items on the drawer, it seems automatically setup the drawer, it's cool. But, each time I click one item on the drawer and click the same item several times, the Nav will navigate to the same fragment and onCreateView will be called each time, it's so SAD! It's definitive a bug because the initializing of a view is not cheap.
Any idea?
Same problem: https://github.com/googlesamples/android-sunflower/issues/117
Meet the same issue. Checked the code , in navigate function inside FragmentNavigator, it use "replace" for any fragment transaction, so it will call onCreate() and onCreateView() every time when we change the destination. Probably we can override Navigator
Hey guys! Have you tried to run the sunflower app? I've tried and noticed the following behavior: when the user navigates from PlantListFragment to PlantDetailFragment and go back, the PlantListFragment is unchanged (holds the same state after the navigation).
The bad thing is: when i try to create my own project with navigation, this behavior doesn't work. When the user goes back all the previous state is lost. Any of you had experienced something like that?
Thanks!
Hey guys! Have you tried to run the sunflower app? I've tried and noticed the following behavior: when the user navigates from PlantListFragment to PlantDetailFragment and go back, the PlantListFragment is unchanged (holds the same state after the navigation).
The bad thing is: when i try to create my own project with navigation, this behavior doesn't work. When the user goes back all the previous state is lost. Any of you had experienced something like that?
Thanks!
That PlantListFragment keeps it's state because it doesn't get destroyed, when you click on an item and navigate to PlantDetailFragment it creates a new instance of DetailFragment and places it on top of the backstack, leaving the ListFragment alive.
When later on you press back, it navigates up, popping the DetailsFragment and leaving the ListFragment visible, meaning ListFragment was never recreated.
Agreed with @aromano272 and @sociaxie. Inability of having proper backstack in nav controller is a huge productivity issue. Hope it's a missing feature or a work-around made on purpose... IMO this is a must-have and has to be introduced sooner rather than later (however: https://issuetracker.google.com/issues/109856764 says _We won't be allowing customization of the transaction type (such as hiding/showing, etc) for the foreseeable future._) :(
@ianhanniballake Can you please share your views on how to achieve the same, possibly with some example?
@ianhanniballake
Temporary solution I think, for BottomNavigationView, you should try NavigationAdvancedSample this example which mentioned in issue tracker for support multiple back stacks. And you should try to keep data in ViewModel and inflate layout with that data when fragment recreate. Most system provided view have already save its state in onSaveInstanceState, and you just need to make sure you keep the right data to restore the view state. For custom view, I think it's you responsibility to make your own SaveInstanceState logic.
@ianhanniballake maybe you can share you views on this issue before you close it
I've just faced the same issue and finally come up with the old way as follow:
1.
currentDestinationId = navController.getGraph().getStartDestination();
2.
navigationView.setNavigationItemSelectedListener(this);
3.
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
final boolean sameDestination = item.getItemId() == currentDestinationId;
if (!sameDestination) {
navController.navigate(item.getItemId());
currentDestinationId = item.getItemId();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
Android navigation component is suck, Better switch to old school method...
has anyone found the solution to it?
Most helpful comment
@ianhanniballake maybe you can share you views on this issue before you close it