61

I'm checking in this way if the file exists, but I need to go beyond, I need to know if there is one in specific, is there any way?

File f = new File("/data/data/com.eventrid.scanner/shared_prefs/Eventrid.xml");
          if (f.exists()){

          }
          else{

          }  
Nico1991
  • 637
  • 1
  • 5
  • 6
  • 1
    Did you check the documentation? Isn't there a `contains` method? – keyser Apr 02 '14 at 19:36
  • method does not exist as a way, my idea is also not read the file, I just wanted to know if there is any easier way. – Nico1991 Apr 02 '14 at 19:42
  • You want to extract information from a file without reading it? – keyser Apr 02 '14 at 19:47
  • 1
    I just want to know if there is a preference, I do not care value, I redirect to another layout if it exists, – Nico1991 Apr 02 '14 at 19:48
  • This method of checking is still useful, if one needs to know if a preference file of the given name (Eventrid in this case) was ever created. I do have a valid use scenario, where this is important to know. Thank you for posting this. – gregko Mar 26 '22 at 17:36

5 Answers5

114

SharedPreferences has a contains(String key) method, which can be used to check if an entry with the given key exists.

http://developer.android.com/reference/android/content/SharedPreferences.html

Yjay
  • 2,717
  • 1
  • 18
  • 11
31

Well, one could do:

    SharedPreferences sharedPrefs = getSharedPreferences("sp_name", MODE_PRIVATE);
    SharedPreferences.Editor ed;
    if(!sharedPrefs.contains("initialized")){
        ed = sharedPrefs.edit();

        //Indicate that the default shared prefs have been set
        ed.putBoolean("initialized", true);

        //Set some default shared pref
        ed.putString("myDefString", "wowsaBowsa");

        ed.commit();
    }  
hdotluna
  • 5,514
  • 4
  • 20
  • 31
Chris Sprague
  • 3,158
  • 33
  • 24
1

Another solution:

If you know the exact number of preferences you have you can:

public void isPreferencesSet(Context context){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return (sharedPreferences.getAll().size() == exactNumberOfPreferences);
}

This works because the preferences file stored in /data/data/myApp/shared_prefs/myApp_prefrences.xml contains a preference's value pair only if its value has been set.

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
1

Check if SharedPreferences key exists:

    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    if(mPrefs.getAll().containsKey("KeyName")){
danyapd
  • 2,516
  • 1
  • 14
  • 23
0

You can try this

 fun checkLoginInfo(): Boolean{
    val saveLogin = sharedPreferences.getBoolean(SAVE_LOGIN, false)
    return saveLogin
}

 Checks whether the preferences contains a preference. 
 @param(SAVE_LOGIN) key The name of the preference to check.
 @param(false) default
 @return Returns true if the preference exists in the preferences, otherwise false.
Cyd
  • 1,245
  • 1
  • 14
  • 17