A
A
Andrey Sklyarov2016-01-26 11:53:47
Android
Andrey Sklyarov, 2016-01-26 11:53:47

How to save state of RecyclerView?

I'll try to explain in as much detail as possible. There are 2 fragments, the first one contains RecyclerView. Its elements are added from the REST service, in batches, page by page. That is, they created a fragment, loaded the first 20 elements, the user scrolls the list, reaches the end, then the next 20 elements are loaded, and so on. This pattern is googled for "endless recyclerview".
By clicking on the list item, we go to the second fragment:

Fragment product = new FragmentProduct();

Bundle bundle = new Bundle();
bundle.putString("PRODUCT", new Gson().toJson(mProductsAdapter.getItem(position)));
product.setArguments(bundle);

getActivity()
  .getSupportFragmentManager()
  .beginTransaction()
  .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
  .replace(R.id.frame_container, product)
  .addToBackStack(null)
  .commit();

This is the usual behavior, from the list of products we open detailed information about the selected product. So, how to get the same state of the RecyclerView when the user clicks the Back button? After all, the first fragment (with the list) is destroyed when the second one (with detailed information about the list element) is opened? For the first fragment, onDestroyView() is called, then onCreateView() and RecyclerView is refilled. Pull from the REST service all the pages that the user managed to scroll? - not an option.
I have 2 ideas:
1. Cache the elements of the list (save them in the database) and when we return to the first fragment with the list - read from the cache, not from the service.
2. Refuse to use the snippet for detailed information about the product. Show it in some dialog, then the fragment with the list will not be destroyed, right?
But all this is somehow crooked, I probably missed some basic thing about working with fragments ... How do you usually solve such a problem?
Update: It should probably also be noted separately that there are no problems with saving the position of the scroll, the question is precisely in saving the content when opening the "child" fragment.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FoxInSox, 2016-01-26
@coder1cv8

Need to cache. The user can leave the application with the home button, and return a few hours later somewhere in the subway where there is no Internet, and then without caching your application will be useless. Well, it's strange to reload data that has already been loaded once.
It doesn't fill itself, you fill it. A link to the created adapter (with loaded data) can be stored in a fragment. And in the onCreateView method, use this adapter again.

S
Sergey, 2016-01-26
@poserge

All of the following is out of my head, so there may be inaccuracies:
The state of fragments, as well as activities, can be saved in the onSaveInstanceState method and restored on a new start. The subtlety is that it must be a fragment with the same ID, otherwise onCreate() will not "see" the saved state, and this ID is not set manually. Therefore, fragments do not need to be explicitly destroyed and, when re-created, their presence must be checked using findFragmentByTag().
In general, fragments are quite tenacious, the system recreates them even if the activity was destroyed and restarted, so I would recommend trying to save the state in onSaveInstanceState and restore it in onCreate ().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question