1

EDIT: OK It turns out this code was working (more or less) I'd left in a line that reset the booleans I was trying to change. Thanks everyone for the help though.

Having trouble using SharedPreferences to read in saved array data when my app starts. My _dPad Boolean and my _FreePlay Integer loads, saves and passes to and from my _renderer without any problems.

The trouble starts when I try and use some arrays easteregg[] only has 2 entries right now so obviously I could just just turn them into separate variables but I wish to add more arras of longer length so this makes a convenient test example.

I've noted on the code what appears to happen (the easteregg[] settings just doesn't appear to have changed)

to read data:

    // Read saved preferences    
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    _renderer._dPad = prefs.getBoolean("_dPad", false);                // * works ok *
    _renderer._FreePlay = prefs.getInt("_FreePlay", 1);                // * works ok *
    _renderer.easteregg[0] = prefs.getBoolean("easteregg[0]", false ); // * not working
    _renderer.easteregg[1] = true;                        // * even this is not working

    setRenderer(_renderer);

to write data:

    public void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    // As good a time as any to save current config
    save = false ; // don't commit if nothing changed.

    SharedPreferences prefs =                       
    PreferenceManager.getDefaultSharedPreferences(getContext());
    SharedPreferences.Editor editor = prefs.edit();

    if (_renderer._dPad != prefs.getBoolean("_dPad",false)){ save = true ;
    editor.putBoolean("_dPad", _renderer._dPad);}

    if (_renderer._FreePlay != prefs.getInt("_FreePlay",1)){ save = true ;
    editor.putInt("_FreePlay", _renderer._FreePlay);}

    editor.putBoolean("easteregg[0]", _renderer.easteregg[0]);

    editor.putBoolean("easteregg[1]", _renderer.easteregg[1]);

    if (save == true){editor.commit();}

}

And in the .renderer class

    // START SAVE DATA
    public boolean _dPad ; // false no Virtual Pad     *Works Fine*
    public int _FreePlay ;  // 1 = no free play        *Works Fine*
    public boolean[] easteregg = new boolean[2];       *Values don't load or save*
    //public boolean easteregg[]; // tried this first  *CAUSES CRASH*
    // END SAVE DATA

Do I have to convert the arrays to strings? I don't get how to change them.

2
  • Booleans are default false if not declared otherwise. Are you sure you've changed the value at some point? Try printing a log statement before you save it and after you retrieve it. Commented Nov 9, 2011 at 18:06
  • well they're staying flase even though I've got "_renderer.easteregg[1] = true;" I know it's not being changed elsewhere in the code because if I change "public boolean[] easteregg = new boolean[2];" to "public boolean easteregg[]; ={true,true}" then the values definitely go true. Commented Nov 9, 2011 at 19:55

3 Answers 3

1

I put your code into a quick activity, creating just the shell of the renderer class as you have above and found that your save boolean is false, so it never commits the preferences.

I forced the save to true, and played around with it and everything worked fine from there.

I'd recommend adding checks to the easter eggs the same as you have for any other preference; test to see if the current value is the same as the saved value, and if not, set the save flag.

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest saving the array as a string in a single variable. It appears you have an array of booleans. So loop through it to make it a series of either ints (0, 1) or the string "true" or "false" then save it to an int or string.

I suspect the probelm might be that your setting name contains square brackets. I think that in key value names, the key name must be a valid variable name. And square brackets are not allowed in variable names.

However i would also expect this to throw an error. Does the code work if you name you settings "easteregg_01" and "easteregg_02"?

3 Comments

Preference keys can be any valid string. So long as all of his easter eggs are going to be booleans, there is no issue with his code (other than the save flag was never getting set ^_^)
d'oh! good catch geekswordsman. And thanks for the answer on the key naming. seems crazy that I could write a key "4*$=throw(new Error()):%8347". But I guess it is so. (Not that I would, but I could)
:-) oh the dangers of showing code fragment. My save flag is getting set
0

The best solutions would be to convert your array into JSON string and store it as preference value. If you have small amount of data, you can as well stick with org.json classes provided by android. If you have more data, GSON pull parser would be better, as it utlizes pull parser. And if you are really lazy, you grab my small databinding library and do:

String jsonState = preferences.getString(GAME_STATE, null);
StateStorage storage = JSONUnmarshaller.unmarshall(new JsonReader(new              
                    StringReader(jsonState)), StateStorage.class);

and it will instantiate java class for you and fill in the data. And to save:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer);

JSONMarshaller.marshall(jsonWriter, ss);
editor.putString(GAME_STATE, writer.toString());
editor.commit();

Databinding library is available on github, or from maven central:

https://github.com/ko5tik/jsonserializer

PS: at the moment I work on injection of preference values ( at the moment primitives only): https://github.com/ko5tik/andject

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.