1. Check out our companion app, Forums for Android! Download from Google Play

Problem with restoring data from pause

Discussion in 'Application Development' started by Jinam, Mar 29, 2010.

  1. Jinam

    Jinam Member
    Thread Starter
    25

    Mar 8, 2010
    5
    0
    25
    I use an array named puzzle of 121 items(11x11) as a map for my Tower Defence, i have 0 for road, 1 for hill, 2 for base and 3,4,5,6,7,8,9 for towers.
    The problem is that when i want to save/restore on onPasue/onResume i have to transform this array into a string because the
    "getPreferences(MODE_PRIVATE).edit().putString(PREF_PUZZLE,
    toPuzzleString(puzzle)).commit();"
    has no putArray so i decided to transform my array into a string with 2 methods 1 from array->string and 1 from string->array listed bellow

    static private String toPuzzleString(int[] puz) {
    StringBuilder buf = new StringBuilder();
    for (int element : puz) {
    buf.append(element);
    }
    return buf.toString();
    }

    static protected int[] fromPuzzleString(String string) {
    int[] puz = new int[string.length()];
    for (int i = 0; i < puz.length; i++) {
    puz = string.charAt(i);
    }
    return puz;
    }
    I get my string with :
    puz = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE,
    easyTD);
    and get my array back(finnaly) with
    fromPuzzleString(puz);

    In "theory" this should work...but when i click the continue button the array is compeltly modified not only in value but in its lenght too.
    Any ideah on where did i go wrong??? or any diffrent ideah on how should i save/restore that array??
     

    Advertisement

Share This Page

Loading...