How to restore Android fragment view state

I have on the screen a fragment of the application headers and content . When the user clicks on an element in the title fragment, the corresponding fragment is created and inserted into the frame, and the highlighted title is highlighted in the title fragment.

The transaction is performed using fragment.addToBackStack() , therefore, when the user presses the BACK key, the previous fragment is restored and inserted into the frame.

What is the best solution to restore view state when a transaction manager restores fragments?

The problem is that I have to highlight the previous fragment name in the header fragment, and I need to know which fragment. I resolved it by saving the state of the view in my own stack: when a fragment is created and restored when a transaction changes, using a transaction listener.

But this does not seem to be the right decision.

+7
source share
1 answer

Before answering the next time, be sure to add your code. Most likely, my answer will not help you as much as possible, because I really do not know your code.

This is old, but in any case, if I understand your question (and the architecture of the application) correctly, it looks like a case of interaction.

Example:

add this interface as a member of the content fragment:

 public class ContentFragment extends Fragment{ public interface onFragmentTitleHighlighted{ public void highLightTitle(String title); } } 

and have a title fragment. Be sure to equip the pieces of content with the fragmnet header and add a call to highLightTitle(String title); in the onCreateView (...) call content snippet. Thus, whenever new fragmnet content is added, the header will be highlighted.

+1
source

All Articles