I am coding an app that requires the ability to save data so the next time a user enters the app their progress will be saved. I am thinking an array with values that will be updated as the user makes choices would be the easiest way to implement this. My first thought was to make a class file with a read and write method so all activities can update and read data from the array but I don't think the array would be permanent when the app closes or I release an update. What is the best to make sure my save array is permanent between app closures and future updates?
-
convert array to json string , store it in shared preferences and get it from shared preferences when app starts – Manohar Jul 18 '20 at 18:05
-
1Does this answer your question? [Is it possible to add an array or object to SharedPreferences on Android](https://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android) – BDL Jul 19 '20 at 11:14
-
This might help you. [save-arraylist-to-sharedpreferences](https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Michael Dec 10 '21 at 00:30
2 Answers
You can do it in several ways.
- Using SharePreferences
//To add or update a data
SharedPreferences sharedPref;
sharedPref = getSharedPreferences("AnyNameForSharedPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", name);
editor.putString("email", email);
editor.putString("phone", phone);
editor.putString("gender", gender);
editor.apply();
//To get the data
final SharedPreferences sharedPref = getSharedPreferences("AnyNameForSharedPref", Context.MODE_PRIVATE);
sharedPref.getString("name", "Default text if it is not present"));
- Using ROOM
Since Room is based on the SQLite database, you can avoid it if don't have lots of data to store. To use ROOM, add these dependencies:
dependencies {
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}
Here's the official documentation for ROOM with example. Android ROOM

- 763
- 8
- 27
you have many options to do this job.
Firstly, you can use SQLite Database (if I were you I would use this).
Create class which extends SQLiteOpenHelper
for example:
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {}
public void onDestroy(SQLiteDatabase database, int oldVersion, int newVersion) {}
}
Here is one example how to create and use SQLiteDatabase
: https://www.tutorialspoint.com/android/android_sqlite_database.htm
Secondly, you can use SharedPreferences
. Maybe, this i simpler way if you have no additional data except your array.
If you want to save your data online you can use Firebase. This is useful when you want to multiple users share data. Here is an example how to use Firebase: https://blog.mindorks.com/firebase-realtime-database-android-tutorial

- 72
- 4