Description
Suppose I have a RecyclerView
that shows 50 photos from extras folder in internal storage. Considering that, That 50 photos are not going to change anytime. So, when user launches the app it fetches 50 photos from internet and saves in internal storage. if user launches photos activity then a cycle of fetching those 50 photos from extras folder will run. This cycle will fetch all the photos from the internal storage and will compare their BUCKET_DISPLAY_NAME
with extras
. If both are equal then and only it will be shown in recyclerView
considering this scenario, I want to save the recyclerView
state
or data
in internal storage. So, when next time user opens app it will not go through fetching all the photos and instead it will direclty load recyclerview with photos which is saved.
Fetch Method
public ArrayList<ImageModel> fetchImages() {
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
cursor = activity.getApplicationContext().getContentResolver().query(uri, projection, null, null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID);
arrayList.clear();
while (cursor.moveToNext()) {
long mediaId = cursor.getLong(column_index_data);
name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
if (name.equals("extras")) {
Uri uriMedia = Uri.withAppendedPath(uri, "" + mediaId);
ImageModel imageModel = new ImageModel();
imageModel.setUri(uriMedia.toString());
arrayList.add(imageModel);
}
}
cursor.close();
return arrayList;
}
As you going through this method may take a lot of time if user has lot of photos other than required 50 photos.
then,
arrayList = fetchImages();
adapter = new PhotosAdapter(requireActivity().getApplicationContext(), arrayList, getActivity(), this, this);
recyclerView.setAdapter(adapter);
Using this method images are loaded in recyclerView
.
So, is there any way to save the loaded RecyclerView
or RecyclerView Data
?
Doubt
According to, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State
State object can also keep arbitrary data, identified by resource ids.
I don't understand much by arbitary data but, if it means recyclerView's
data then How can I use RecyclerView.State
object to restore the data inside recyclerView
?
Note
Assume I can save any type of object in internal storage and load that object into another same class's object.
Edit
As mentioned in answer loaded recyclerView cannot be stored. Instead data needs to be cached. As cache memory is inside the internal storage as well it is forcing my app to skip frames because I am loading images inside onCreateView()
of my app. I can create a new Thread it will handle it in background and there are no skipped frames. But, It creates a blink effect which i don't want.