Reputation: 341
I have already published my game on Android but I did not build it in libGDX. Now I am rebuilding the game and I have run into a big problem I am hoping somebody can help me with.
I originally used Android's SharedPreferences to save data I will now be using libGdx Preferences. This will be fine for people that have never downloaded the game. But I am concerned for the people that have already downloaded it (I do not want them to lose their saved data). Is there an easy way to get this data in my new version of the game?
UPDATE
I was able to use the EXACT same code. Since LibGDX's Preferences is based on SharedPreferences. It was wonderful. I just hope it works when moving it over to iphone.
Upvotes: 1
Views: 456
Reputation: 25177
You should be able to read the old SharedPreferences
in the Android backend for your game (you can use arbitrary Android code in there).
You'll want to set things up using the Libgx "Platform Specific code" approach, so your shared code can call into the Android code. (See the wiki page for more details: https://code.google.com/p/libgdx/wiki/ApplicationPlatformSpecific).
There are several ways to structure this, but I think you should first create an interface that contains a method like upgradeOldPreferences
. Pass a reference to a class that implements that interface into your ApplicationListener
's constructor on each backend (the class is a no-op on the other backends). On the Android backend you'll want to check the "new" preferences to see if you need to convert existing preferences. If you do, then read them in using Android-specific SharedPreferences
code. You can use Libgdx APIs in the Android back end, so you can either write out Libgdx preferences, or just pass the resulting data back to your game (e.g., as the return value for the interface method). At this point you'll probably want to persist a flag in the new preferences saying you've already converted the old prefs.
Alternatively, you could rely on the fact that Libgdx on Android uses SharedPreferences
as its backend (see AndroidPreferences.java) and more directly migrate the old preferences into the format used by Libgdx.
Upvotes: 2