1

Upon reinstalling my app the code within my if statement is being accessed despite my SharedPreferences entry not yet having been created. I'm using an emulator with eclipse, does the data need to be cleared some other way than reinstalling? Thanks

prefs = getSharedPreferences("appData", 0);
        Gson gson = new Gson();
        String gsonStr = prefs.getString("playerString", null);

        if(gsonStr != null)
        {
            //This code is being accessed on the apps first onCreate() call prior to being reinstalled 
            Player[] tempArray = gson.fromJson(gsonStr, Player[].class);
            Log.d("First Player", "" + tempArray[0]);
        }

protected void onPause() 
{
    super.onPause();
    if(savedPlayers != null)
    {
        Gson gson = new Gson();
        String gsonStr = gson.toJson(savedPlayers.toArray());

        prefs = getSharedPreferences("appData", 0);
        SharedPreferences.Editor editor = prefs.edit();
        //editor().clear();
        Log.d("GSONString",gson.toString());
        editor.putString("playerString", gsonStr);
        editor.putInt("ArraySize", savedPlayers.size());
        editor.commit();
    }
}
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72

2 Answers2

5

Most likely the app is simply being reinstalled. In order for the data to be cleared, the app actually has to be uninstalled first. Reinstalling won't actually clear the data. The emulator actually allows for a runtime option that will wipe all user data when you run the emulator, but I don't believe it runs each time your run the app. You can set this option in the Run Configurations for the app if you are using eclipse.

To do it from inside the emulator, go to settings->applications->manage applications->your app->clear data

ootinii
  • 1,795
  • 20
  • 15
3

A shared preference and sqlite db doesnt get removed on a reinstall. To delete them, go to Settings-->Appications-->Manage Applications-->Click on you application-->Click on "Clear Data" on emulator. this will clear the data stored.

Akhil
  • 13,888
  • 7
  • 35
  • 39