Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getDefaultSharedPreferences

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.

like image 275
Kostadin Avatar asked May 28 '12 14:05

Kostadin


People also ask

What is getDefaultSharedPreferences?

getDefaultSharedPreferences() is used to get the shared preferences that work in accordance with Android's overall preference framework. getDefaultSharedPreferences() is better to use as it gives the SharedPreferences object that works with a PreferenceActivity by default.

Where are shared preferences stored Android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

What is the difference between getPreferences () and getSharedPreferences ()?

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.


1 Answers

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
like image 199
Behzad Momahed Heravi Avatar answered Oct 04 '22 00:10

Behzad Momahed Heravi