why

The reason is that the Activity saves the fragment state when onSaveInstanceState, and restores the saved fragment state when the Activity is rebuilt. When oncreat is implemented, it is often added again. There are many solutions available on the Internet, and individuals use methods in their projects that they don’t seem to see many people using

To deal with

Restore the Fragment in onAttachFragment(Fragment Fragment) of the Activity

if(fragment instanceof FragmentA){
  fragmentA = fragment;
}
if(fragment instanceof FragmentB){
  fragmentB = fragment;
}
Copy the code

Created in the Activity’s onCreate

if(fragmentA == null){
  fragmentA = new FragmentA();
}

if(fragmentB == null){
  fragmentB = new FragmentB();
}
Copy the code

With that done…

Looking at many other ways to solve this problem, I personally think the above treatment is simple and crude… If you have any questions, please point them out. Okay