Hi there! I'm delighted to share with you today a useful tip I discovered while working on an Android app project: leveraging Android Studio's SharedPreferences to save the state of RecyclerView. This tutorial is for you if you've ever had trouble keeping your RecyclerView in its current state when your app restarts or undergoes configuration changes. Now let's get started!
First, Configure RecyclerView
First things first, confirm that your app is configured with a RecyclerView. Don't worry if you're unfamiliar with RecyclerViews; they're just an easier and more effective way for Android apps to display lists of data. There are a ton of internet instructions available to assist you in setting one up. That Process would require you:
Add Recycler View to Xml or activity Layout.
Make an Adapter Class with a Model Class
Initiate Adapter and Recycler view in activity
It's a Long Story Doesn't End Here but I am just telling you how to save items of recycler view in Shared Prefs.
Second, Importing SDK
Import this sdk down below in your build.gradle (Module). You may update this in future.
implementation 'com.google.code.gson:gson:2.10.1'
Third, Saving & Retrieving Method in Adapter Class
For this, you have to write down these methods in your adapter class Nicely
public void saveData() { SharedPreferences sharedPreferences = context.getSharedPreferences("MyItems", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); // Convert the list of items to a JSON string Gson gson2 = new Gson(); String listJson = gson2.toJson(dataSet); // Save the JSON string to SharedPreferences editor.putString("listItems", listJson); editor.apply(); } public void loadData() { SharedPreferences sharedPreferences = context.getSharedPreferences("MyItems", Context.MODE_PRIVATE); String itemsJson = sharedPreferencesideas.getString("listItems", null); if (itemsJson != null) { // Load the data from SharedPreferences and update the itemList Gson gson2 = new Gson(); Type type = new TypeToken<List<DataModel>>() {}.getType(); List<DataModel> savedItemList = gson2.fromJson(itemsJson, type); dataSet.clear(); dataSet.addAll(savedItemList); notifyDataSetChanged(); } }
Fourth, Code to Call these Methods
In this area of code you have to add Main activity reference so that we can call above methods in Main Activity's resume & Pause Method to ensure proper saving and retrieving of data. (Make sure make public variable of "MainActivity mainActivity" in Adapter Class, also cahnge names of dataSet, DataModel and Activity Accordingly as you defined them).
public ListAdapter(Context context, List<DataModel> dataSet, MainActivity mainAct) { this.context = context; this.dataSet = dataSet; this.mainActivity = mainAct; }
After this code make sure also make changes when initiating adapter in your activity.
Make sure also make a public variable of listadapter at Start of the Activity Like "ListAdapter listAdapter;" and below in the we added reference of this activity by "this".
listAdapter = new ListAdapter(this, ideasDataList, this);
Fifth, Calling Methods on onResume() & onPause()
The reason why i am calling these save and retrieve methods on resume and pause is because even if the user closes, minimize or move to any other actiivity the on pause method takes place, on resume is opposite of it.
@Override protected void onPause() { super.onPause(); listAdapter.saveData(); } @Override protected void onResume() { super.onResume(); listAdapter.loadData(); }
In conclusion, this is a brief and simple guide that explains how to use Android Studio's SharedPreferences to save and restore the state of your RecyclerView. You can make sure that your app offers a flawless user experience even if the user navigates away from it and returns later by following these easy steps. Have fun with coding!