I’m making a settings screen using an int to set the background of my app. It works just fine… inside the current activity. But as soon as I leave the activity, the int value is lost and the background isn’t changed.
what I want to do: I want to save the int from my settings activity, and then import it to my other activites and check if the int “bak” equals null, “bg”, “bg1” or “bg2”.
I’ve heard of sharedPreferences but never got it working. That is why I open a new thread.
OK, add these global variables
initialize it in onCreate
then to add something to it, use this, with
"key"being a unique descriptor andnamebeing the variable name you want to storeaccess it by this, where
defaultis what you want the variable to be assigned if no preference exists.EDIT:
I noticed you want to use letters, eg bg1 etc. To do that you need to use a String, or use an int with a switch case or multiple if statements. Here’s a switch case example you can modify. Just make sure to put the switch case statement after you access the SharedPreference in the previous code block.
Just add as many case statements as needed.