'Is there any way to avoid calling The onCreateView method when back to the previous fragment?

Let's suppose I have A and B fragments, The A fragment contains 9 fields and a button, This button will navigate me to the B fragment.

After navigating to the B fragment, I decided back to the A fragment via the back button that exists in the toolbar but the problem is A fragment will call the onCreateView method again, In this situation, I lost fields that I inserted it and I lost the current position of the scrolling.

Is there an official way found in the Navigation Component library to solve the problem?

I watched fragment lifecycle but is there any way to avoid calling The onCreateView method when back to the previous fragment?



Solution 1:[1]

You can add a boolean value which get's true value in the first onCreateView and check it in this method. OR you can check if something what you do in onCreateView was already done and put all you logic in an IF statement so it will work only one time. It's just the simplest solution.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Marina