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.

Kostadin
  • 2,499
  • 5
  • 34
  • 58
  • 2
    It's not unheard of for some devices to randomly lose SharedPreferences data. See http://code.google.com/p/android/issues/detail?id=14359 for an example. – Graham Borland May 28 '12 at 14:51
  • Is this info related only to getDefaultSharedPreferences? Do you know same issues about getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE); ? – Kostadin May 29 '12 at 08:03
  • try with getSharedPreferences than the default one. It works fine – png May 29 '12 at 08:36
  • in getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); say warning and deprecated!what we do now? – Dr.jacky Oct 28 '12 at 06:10
  • 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://stackoverflow.com/a/37953072/4410376 – Hack06 Mar 23 '18 at 22:52

2 Answers2

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
mixel
  • 25,177
  • 13
  • 126
  • 165
Behzad Momahed Heravi
  • 1,383
  • 1
  • 12
  • 17
  • 3
    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. – Kostadin Jun 04 '12 at 07:45
5

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
user2288580
  • 2,210
  • 23
  • 16