Thursday, 15 September 2011

java - Shared Preferences not saving when I compile my code -



java - Shared Preferences not saving when I compile my code -

i using sharedpreferences store players highscore. highscore function works, , highscore saves, when create alter code, players highscore reset 0. worried because if have update in future restart players scores.

public void highscoresaver() { if(finalscore > sharedprefmanager.gethighscore()) { sharedprefmanager.sethighscore(finalscore); sharedprefmanager.storetopref(); } }

sharedprefmanager.java:

package com.divergent.thumbler; import android.content.context; import android.content.sharedpreferences; // methods static , can phone call in code //all fellow member variables private, can save load our own fun public class sharedprefmanager { //this shared preference file name, in save info public static final string my_emp_prefs = "mysharedpref"; //saving context, can phone call //shared pref methods non activity classes. //because getsharedpreferences required context. //but in activity class can phone call without context private static context mcontext; // user input in below variables, store in shared pref private static int highscore; public static void init(context context) { mcontext = context; } public static void loadfrompref() { sharedpreferences settings = mcontext.getsharedpreferences(my_emp_prefs, 0); // note here 2nd parameter 0 default parameter private access, //operating mode. utilize 0 or mode_private default operation, settings.getint("highscore", highscore); } public static void storetopref() { // existing preference file sharedpreferences settings = mcontext.getsharedpreferences(my_emp_prefs, 0); //need editor edit , save values sharedpreferences.editor editor = settings.edit(); editor.putint("highscore", highscore); // age key , mage holding value //final step commit (save)the changes in shared pref editor.commit(); } public static void deletesingleentryfrompref(string keyname) { sharedpreferences settings = mcontext.getsharedpreferences(my_emp_prefs, 0); //need editor edit , save values sharedpreferences.editor editor = settings.edit(); editor.remove(keyname); editor.commit(); } public static void deleteallentriesfrompref() { sharedpreferences settings = mcontext.getsharedpreferences(my_emp_prefs, 0); //need editor edit , save values sharedpreferences.editor editor = settings.edit(); editor.clear(); editor.commit(); } public static void sethighscore(int score) { highscore = score; } public static int gethighscore() { homecoming highscore ; } }

you're never setting highscore integer value. think maybe think line doing different is:

settings.getint("highscore", highscore);

this calls getint function, returns value has key in first paramater. if value doesn't exist in shared preferences, returns highscore instead. want this:

highscore = settings.getint("highscore", 0)

this set highscore value in settings under "highscore". if there no value, it'll homecoming 0.

make sure phone call loadfrompref() if you're going phone call gethighscore()!

java android sharedpreferences

No comments:

Post a Comment