Android getDefaultSharedPreferences
Asked Answered
E

2

63

My code is:

final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean hasBeenShown = prefs.getBoolean(eulaKey, false);

Always returns different values depending on os version. Tested in 2.2, 2.3.4, 3.2, 4.0.3 - returns correct value. But for device Zte blade with 2.3.7 with CianogenMod 7.1 - result is always false. I suppose default value for getBoolean.

Here is code writing boolean:

final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();

Does anybody have any idea?

Update: Comparing my current code with my previous version of code - there is no difference in code. Only difference is in manifest: code works Ok with minVersion=8 and targetVersion=8 Now I'm compiling with minversion=8 and target=13 /because of Admob/. Maybe some APIs changed, but I found nothing on this.

SOLUTION: -Starting app from shortcut and from menu gives me different DefaultSharedPreferences. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people don't make shortcuts, so I had to change code.

Embarkment answered 28/5, 2012 at 14:19 Comment(5)
It's not unheard of for some devices to randomly lose SharedPreferences data. See code.google.com/p/android/issues/detail?id=14359 for an example.Cloudlet
Is this info related only to getDefaultSharedPreferences? Do you know same issues about getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE); ?Embarkment
try with getSharedPreferences than the default one. It works fineExclamatory
in getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); say warning and deprecated!what we do now?Angi
Hmm... it seems like OS versions mess up with package names. See my Q&A related to the difference of those functions of shared-prefs: https://mcmap.net/q/66515/-mess-with-the-shared-preferences-of-android-which-function-to-useArdra
G
65

Try it this way:

final String eulaKey = "mykey";
Context mContext = getApplicationContext();
mPrefs = mContext.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();

in which case you can specify your own preferences file name (myAppPrefs) and can control access persmission to it. Other operating modes include:

  • MODE_WORLD_READABLE
  • MODE_WORLD_WRITEABLE
  • MODE_MULTI_PROCESS
Gq answered 28/5, 2012 at 15:4 Comment(1)
Starting app from shortcut and from menu gives me different DefaultSharedPreferences. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people dont make shrotcuts, so I had to change code.Embarkment
T
6

If you've upgraded to targeting API 30 drop this in your gradle dependencies:

implementation 'androidx.preference:preference-ktx:1.0.0'//For Kotlin Projects

implementation 'androidx.preference:preference:1.1.1'//For Java Projects

After re-synching Gradle change all of your imports from

import android.preference.PreferenceManager

To

import androidx.preference.PreferenceManager
Tricotine answered 12/3, 2021 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.