How to Save Variables in SharedPreferences - Android Studio Tutorial

Coding in Flow · 223K views · 2017 · 11:34

Open on YouTube

Software (other)Web developmenttutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about persisting simple key-value data across app restarts or screens in Android with SharedPreferences.

Recommended by 1 people across 1 different months, 2018 to 2021.

What it is

A short Android Studio tutorial from Coding in Flow that shows how to store data with SharedPreferences. It uses SharedPreferences.Editor to save Strings and Booleans, and notes that Integers, Floats and Longs work the same way, with example code on GitHub.

What people said

Recommends Luke C · 2018-02 · Stack Overflow · on “store variables to order them in another android layer”

I can't tell if "This method make with Shared Preferences?" means you have or you haven't tried using SharedPreferences, but I'll assume you haven't. SharedPreferences store in key-value pairs. So I think this would be a great option for you to use. There's many great tutorials, but a great one is this: [this video] And here is a link to the docs for more: [LINK] Hope it helps :)

Recommends Luke C · 2018-02 · Stack Overflow · on “How to fetch data after the app is restarted in android studio?”

SharedPreferences should do the job. They store information in a key-value pair even when the device is turned off or restarted, and of course when the app is closed out of. Here's a link to the docs: [LINK] Here's a useful tutorial that I learnt best from: [this video] Hope it helps :)

Cites Sam Chen · 2020-12 · Stack Overflow · on “Problem when stored more than one int key values in sharedpreferences, any solutions?”

For storing different values, you must use different key. Try this: private lateinit var sharedpreferences: SharedPreferences val bgColorKey = "bgColor" //key must be a `String` type, required by SharedPreference val textColorKey= "textColor" //different thing, diffrent key, they're must be separated //SAVING CODE: val editor = sharedpreferences.edit() editor.putInt(bgColorKey, R.drawable.col1) //the value you store is a resource id, Int type editor.putInt(textColorKey, R.color.white) editor.commit() //PRINT LINE CODE: println("bgColorKey value = "+sharedpreferences.getInt(bgColorKey, 0)) println("textColorKey value = "+sharedpreferences2.getInt(textColorKey, 0)) SharedPreferences tutorial:

Cites Vishal A. · 2021-12 · Stack Overflow · on “Save singleChoiceItems AlertDialog option using SharedPreference”

I was able to solve the problem by sharing the string related to the theme in sharedPreference . I also implemented the AlertDialog issue where I wanted to check the items which was previously chosen inside the app. Following is the code I used: String selectedTheme = "Light"; int checkedItem = 0; public static final String SHARED_PREFS = "sharedPrefs"; public static final String TEXT = "text"; public static final String RADIO = "selected"; Then I called loadData() and updateViews() from onCreate() . public void openDialog() { String[] themes = {"Light", "Dark", "System default"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Choose Theme"); if (