Yousif Al-Raheem
Yousif Al-Raheem

Reputation: 37

Having difficulty understanding shared preferences

I've been searching all over the internet about shared preferences for android. I'm working on an application where I need to store a lot of data. I have three important questions, please try to answer them with clear explanation (short and informative is best):

  1. How can I set initial values(default values), if you put the editor in Splash screen then you will reset the values every time the user launches the application.
  2. Using getSomething() always get you the value you put in it, like if I say getBoolean(first_start,true) it will return true, what if I wanted to get the value stored in the file, how do I do that??
  3. can I create sharedpreferences file (txt) and put default values in keyvaluepair format and put it inside the apk, so that when the user installs the app gets default values??

Thank you in advance for wasting my time on me :P

Upvotes: 0

Views: 319

Answers (3)

degratnik
degratnik

Reputation: 830

        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

//if value for first_run does not exist. That we have first run of application.
//prefs.getBoolean("first_run", true); will return default value true
    Boolean first_run = prefs.getBoolean("first_run", true);
    if (first_run) {
       Editor editor = prefs.edit();
       // after that Boolean first_run = prefs.getBoolean("first_run", true);
       // will always return false
       editor.putBoolean("first_run", false); 
       // do some on first app run
    } else {
       // do some if not first run
    }

Upvotes: 0

SilentKnight
SilentKnight

Reputation: 14021

Look at this simple sample first:

SharedPreferences sharedPreferences = getSharedPreferences("name", Context.MODE_PRIVATE);

A SharedPreference stores pairs of key-value in xml files. The simple sample above will generate an xml file called 'name.xml' if this file doesn't exist under the directory of '/data/data/<package name>/shared_prefs' where is all SharedPreference files exists.

float aFloat = sharedPreferences.getFloat("float", 0.0f);

And this line means: you want to get a float value stored in the xml file 'name.xml'. If you ever stored the float value whose name is 'float' in the 'name.xml' file, it will return the value you stored;if not, it will return 0.0 which is the default value of 'float'. So, 1,2,You don't need to initialize a default value intentionally. You can set the default value by the second line code. 3,you can't create a SharedPreference file in the format of '.txt' and you don't need to do this. All about SharedPreference would be store at '/data/data/<package name>/shared_prefs' in xml. Hope this will help you.

Upvotes: 0

degratnik
degratnik

Reputation: 830

1 - Put some flag in shared preferences that will be set after initial values setting. And put flag checking in Splash screen.

2 - You can use assets - Where do I place the 'assets' folder in Android Studio?

3 - Yes. You can put files in assets and read it on first run and set initial values in shared preferences.

Upvotes: 1

Related Questions