Vivekanand
Vivekanand

Reputation: 755

Check if key exists in Shared Preferences

I'm creating Shared Preferences as follows

preferences = getSharedPreferences("text", 0);
final Editor editor = preferences.edit();

String s1 = serverIP.getText().toString();
String s2 = serverPort.getText().toString();
String s3 = syncPass.getText().toString();
String s4 = proxyServer.getText().toString();
String s5 = proxyPort.getText().toString();

editor.putString("SERVERIP", s1);
editor.putString("SERVERPORT", s2);
editor.putString("SYNCPASS", s3);
editor.putString("PROXYSERVER", s3);
editor.putString("PROXYPORT", s3);

and onCreate I want to display the values in a new set of TextViews, but the first time I don't have any values stored in the shared preferences and will get a NULL Pointer exception.

I want to know if there is any built-in method which can check if the SharedPreferences contains any value or not, so that I can check if the key exists and if not, then replace the new set of TextViews with the preferences value.

Upvotes: 53

Views: 79685

Answers (5)

Yosidroid
Yosidroid

Reputation: 2233

I know this is a late answer but for those who want to know if a shared preference is either empty or zero size, you can check it two ways.

            preferences = getSharedPreferences("text", 0);
            Map<String, ?> entries = preferences.getAll();//get all entries from shared preference
            Set<String> keys = entries.keySet();//set all key entries into an array of string type

            //first option
            if(keys.isEmpty()){
                //do your staff here

            }

            //second option
            if(keys.size()==0){
                //this shows that it is empty as well
                //do your staff here

            }

           //Below is extra
           //If you want to get the names of each keys also, you can use
           //For each loop as well
           //Go through the set of keys
           for (String key : keys) {

               String keyName = key;//get each key name
           }

Upvotes: 0

mirazimi
mirazimi

Reputation: 880

    LoadRuns();
if (loadedruns == 1) {
Toast.makeText(MainActivity.this, "First run", Toast.LENGTH_SHORT).show(); 
}
else {
Toast.makeText(MainActivity.this, "No. runs: " + loadedruns,
Toast.LENGTH_SHORT).show();
}
loadedruns++;
SaveRuns("runs", loadedruns);


    public void SaveRuns(String key, int value){
sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public void LoadRuns(){
sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
loadedruns = sharedPreferences.getInt("runs", 1);
}

Upvotes: -1

Joshua G
Joshua G

Reputation: 2136

Try contains(String key) Accorting to the Javadocs,

Checks whether the preferences contains a preference. Returns true if the preference exists in the preferences, otherwise false.

Upvotes: 152

Richa
Richa

Reputation: 3193

Try out

 SharedPreferences shf = getSharedPreferences("NAME_SharedPref", MODE_WORLD_READABLE);
    String strPref = shf.getString("SERVERIP", null);

    if(strPref != null) {
    // do some thing

    }

Upvotes: 12

Mojo Risin
Mojo Risin

Reputation: 8142

Every method for fetching values from SharedPreferences has default value which is returned in case the key does not exist

preferences = getSharedPreferences("text", 0);
String value = preferences.getString("unknown_key",null);
if (value == null) {
    // the key does not exist
} else {
    // handle the value
}

Upvotes: 24

Related Questions