'Local variable not restored on orientation change for fragments in backstack

I have two fragments, FragmentA and FragmentB, the user can go from FragmentA to FragmentB

Let's say FragmentA has a local variable varA. When the user is on FragmentA and changes orientation I can save that variable with onSaveInstanceState and then restore it in onViewCreated. No problem there.

However, when the user is on FragmentB and the orientation changes, onSaveInstanceState is called for FragmentA, but onViewCreated is never called and the variable is never restored.

How can I restore the variable properly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source