Answer the question
In order to leave comments, you need to log in
How to save fragment state when replace is called?
I have a task to create a preview of the entered data.
To solve this problem, I call a new fragment from the current fragment, using the replace method. The user exits the preview with the "Back" button (for now, but this function should remain).
As it turned out, during such actions, onSaveInstanceState is not called for the current fragment, as a result, only the data entered in the fields with id is saved.
Tell me how to save the state of the fragment in this case?
U.P. For the current fragment, I call addToBackStack(null);
Answer the question
In order to leave comments, you need to log in
In my opinion, replace should not be used here at all. You need to add to the preview fragment, it will be displayed on top of the current one, and the state of the current one will not change.
The onSaveInstanceState method is called when the configuration is changed, when the fragment is deleted by calling replace, no configuration change occurs, so the state is not saved or restored.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question