问题
I am using android studio navigation component to create some fragments and switch between them using the actions. Problem is that each time I tap Back button to go back from a Fragment2 to Fragment1, Fragment one is recreated.
I want to avoid that.
I found solutions when one don't use navigation component and simply can replace ".replace" with ".add", but can't find any information on how to do the same with navigation component.
回答1:
I think it's a normal thing that your fragment 1 is recreated, I think you have a fragment container. And when you move from fragment 1 to fragment 2, you replace the fragment 1 with fragment 2 layout using FragmentTransaction.commit() If you want to control the back button of your App, you can override the Onbackpresed method in your Mainactivity class. what specifically you want to do when you press the back button ??
来源:https://stackoverflow.com/questions/54266152/how-to-avoid-fragment-recreation-when-tap-back-button-using-navigation-architect