Answer the question
In order to leave comments, you need to log in
android app. Fragment as a self-contained module/control?
I am writing an application for memorizing English words (mostly it is interesting to deal with the android platform itself), I encountered a problem or a misunderstanding.
There is a "lesson" activity that knows how to get a list of words to display. There are several fragments that are directly responsible for the display of words (in various ways) and user interaction.
I want to make each fragment as independent as possible, i.e. I created a fragment in the code, added it to the UI, and passed the list of words. In the final, the fragment returns the results to the activity and it is replaced by another one.
Problem
In the process of screen rotation, the activity is recreated, and with it the current fragment, as a result, everything starts from the beginning. I don’t want to save the progress of the fragment in the activity, it breaks the encapsulation.
I know about setRetainInstance, I couldn’t get it to work completely, plus they write on developer.android.com that this is more for fragments where there is no UI, but I practically only have it.
Interested in an architecturally correct solution to the problem. Thank you.
Answer the question
In order to leave comments, you need to log in
After a little research on the behavior of Fragment with setRetainInstance and without it, I realized that these are not cats that are not tasty ..
A simple experiment with outputting events from the fragment to the log when rotating helped to figure it out.
From the log, it is perfectly clear that the fragment is not destroyed (only its UI), as a result, all data (list of words, current position, intermediate results) remain in their places and you only need to redraw the UI based on this data.
I consider this option more convenient than using onSaveInstanceState.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question