Stefan
Stefan

Reputation: 31

Apparent SharedPreferences problem under Android 13 (API level 33)

I use the SharedPreferences API to save and retrieve preferences in my Android app.

    // Retrieve preferences
SharedPreferences settings = getSharedPreferences("MyPreferences", MODE_PRIVATE);
mnSpinnerPos = settings.getInt("CAT_POS", 0);
munSpinnerVal = settings.getLong("CAT_VAL", 1);


// Set preferences
SharedPreferences settings = getSharedPreferences("MyPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("CAT_POS",mnSpinnerPos);
editor.putLong("CAT_VAL", munSpinnerVal);
editor.apply();

I use the preference to set the default selection in a spinner control. Under prior Android version (e.g. Android 9) this works perfectly fine.

However, when I install it on an Android phone with Android 13, it seems to work fine for a few days, and then it does not work anymore. Instead of defaulting to the specific spinner value it defaults to the first value (mnSpinnerPos=0).

As long as I let the app run in the background, the preferences are retrieved and the spinner is initialed correctly. But when I "force close" the application via "close all" the problem occurs. Note that the problem only starts after a few days: When I force close the app after fresh install it still works for a while (couple of days)

I tested this in the emulator multiple times, but there it always works. Only when I install the release version on the physical phone I run into problems.

Who can help me?

Thanks so much!

Upvotes: 1

Views: 415

Answers (0)

Related Questions