Answer the question
In order to leave comments, you need to log in
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();
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question