0

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

3 Answers3

1

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.

Community
  • 1
  • 1
degratnik
  • 830
  • 2
  • 9
  • 19
  • Thank you for your answer, I've started creating assets, but I couldn't understand the first point, putting a flag on shared preferences is just like putting a string value in it right? if so, it is still unsolved because how can I put a string with a default value to be read for the first time?? can you give me an example of how to set "first_time" boolean value? or "signed_in" boolean value? – Yousif Al-Raheem Mar 30 '15 at 07:54
0

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.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • Ok, so if I wanted to check first time launch I would put {Boolean firstTimeChecker = sharedPreferences.getBoolean("first_time",false); if(firstTimeChecker) skipTutorial(); else startActivity(tutorial_intent);} the false I've given will be set as default if there is no value created but afterwards when I change the value to true, it will never go back to false until the user clears the app data, can you confirm if I understood it right – Yousif Al-Raheem Mar 30 '15 at 08:02
  • @YousifAl-Raheem Hey, man, you can't accepted the answer above! – SilentKnight Mar 30 '15 at 08:24
  • sorry I was trying to accept both, it turns out you can only accept one answer, looool sorry – Yousif Al-Raheem Mar 30 '15 at 08:35
  • By the way, problem solved and the app is working perfectly, I really appreciate your help :D – Yousif Al-Raheem Mar 30 '15 at 08:36
  • @YousifAl-Raheem Oh, Congrats. – SilentKnight Mar 30 '15 at 08:40
0
        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
    }
degratnik
  • 830
  • 2
  • 9
  • 19