How to Save an ArrayList of Custom Objects to SharedPreferences with Gson - Android Studio Tutorial
Why Stack Overflow recommends it
Stack Overflow users link it when someone needs to persist or pass a list of custom objects in Android and a Gson-to-SharedPreferences approach is the simplest fix.
Recommended by 2 people across 3 different months, 2017 to 2023.
What it is
A short Android Studio tutorial by Coding in Flow showing how to store an ArrayList of custom objects in SharedPreferences. It uses Google's Gson library to turn the list into a JSON string, saves it with putString, and reads it back, with a RecyclerView example app.
What people said
Recommends jonasxd360 · 2017-11 · Stack Overflow · on “How do I send an ArrayList of Object though Intent?”
You might want to consider saving the Arraylist to SharedPreferences and pass the key to it through an intent extra. I experienced it to be the easiest to use Gson and json to write an Arraylist of Custom Objects to SharedPreferences. Here is a good tutorial on how to do that: [this video] But if you want to use parcelable I would recommend you to watch this tutorial: [LINK] I hope this could help you!
Recommends jonasxd360 · 2018-07 · Stack Overflow · on “Save data in an activity”
You could save images or large data to the internal storage (android doc) [LINK] or save other kinds of smaller data such as arraylists, objects into shared preferences with gson (for that see this tutorial [this video] ). Hope this helps!
Recommends StanislavOssovsky · 2021-03 · Stack Overflow · on “Saving state of custom object ArrayList”
Basically speaking, you need to implement two methods: saveData() and loadData(). These will use GSON library in order to transform any object into a string and then re-create them back. Then you save and restore your objects where ever you need. You may do so when onPause() method is called, or anywhere else. Here is the code: Firstly, add the following dependency line in your build.gradle: implementation 'com.google.code.gson:gson:2.8.6' Then you write those two methods private void saveData(){ SharedPreferences sharedPreferences = getSharedPreferences("shared preferences id", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); Gson gson = new Gson(); String json = g
Cites gpilka · 2022-01 · Stack Overflow · on “Android Studio - How to implement this in an own class? (Shared Preferences)”
I have a Class named "Movie" which has attributes like "title" and "length" etc. I managed to insert the wanted values using an user interface into some Editboxes. I wrote a function addMovie which created the Movie-Object which i added into a Movie-ArrayList. After that i wanted to make the ArrayList Data persistent. I searched a little and have found this video : [this video] The creator used SharedPreferences to store the data inside the App-Storage. However he wrote the save- and load-methods directly into the Class where he used them. I wanted to create a class specifically for Saving and Loading purposes. So i made this(DataManger.java): package com.example.watchassistant; import android.co
Cites Luke Mills · 2023-01 · Stack Overflow · on “Passing an XML vector image between activities”
I discovered that you cannot send things back with the putExtra when resuming an activity... I dont know why this is, I thought the onResume() would fix this but apparently not. There is also an onNewIntent() that supposedly also corrects this but I couldn't get that working either. Same again for startActivityForResult() which has been deprecated... Anyways, if you.. future human.. also have this problem I have solved it by saving the data i want as a part of a class in an arraylist of size 1. This is the tutorial I followed, I am using this code elsewhere already so I didnt have to change much, but it might be over the top if you're looking for a simple solution. [this video]