• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with restoring data from pause

Jinam

Lurker
Mar 8, 2010
5
0
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??
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones