0

I want to clear the data I am getting from SharedPreference, I try the following answer but didn't make my task:

1) how to delete sharedpreferences ,Quit and launch application from first actvity in android

2) clear the value of sharedpreferences

3) Remove Shared preferences key/value pairs

4) SharedPreferences Clear/Save

they are all removing value, after they write data into SharedPreference, like editor.remove and .clear...

I have write data into SharedPreference in Notification Activity Like this:

public static final String PREFS_NAME = "com.example.sociapp";

NotificationAdapter notificationAdapter1 = new NotificationAdapter(NotificationsActivity.this, NotificationList, NKeyList);
                    RnotificationList.setAdapter(notificationAdapter1);
                    isthereItem = notificationAdapter1.getItemCount();
                    Toast.makeText(NotificationsActivity.this, ""+isthereItem, Toast.LENGTH_SHORT).show();

                    //writing data into SharedPreference
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putInt("changingicon",isthereItem);
                     //editor.commit();
                    editor.clear();
                    editor.apply();

And I am getting this int value in MainActivity Like this:

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

 // Reading from SharedPreferen  
try {
//all I want to refresh below line everytime I start MainActivity.
      int ChangeIcon = settings.getInt("changingicon", 0);
      if (ChangeIcon == 0)
      {
          int valuebecomes = 0;
          notificationIconSetting(valuebecomes);
      }
      else
          {
              int valuebecomes = 1;
              notificationIconSetting(valuebecomes);
          }

      Toast.makeText(MainActivity.this, ""+ChangeIcon, Toast.LENGTH_SHORT).show();

  }
  catch (ClassCastException e){e.printStackTrace();}

The method I call, when I get int value from SharedPreference:

 private void notificationIconSetting(int IconTochange)
    {
       if (IconTochange == 0) {

           navigationView.getMenu().getItem(2).setIcon(R.drawable.notification);
       }
       else
           {
               navigationView.getMenu().getItem(2).setIcon(R.drawable.notificationalert);
               navigationView.setItemIconTintList(null);

           }
    }

Actually I am getting an int value greater than 0 when there is a notification in the adapter, and when there is no notification in the adapter the int value is equal to 0, then I am using this value to change the notification icon.

When there is notification:

screenshot

When there is no notification:

screenshot

Now the problem is whenever I get a value, it remains the same until I clear app cache or Uninstall and then install again. All I want to refresh the SharedPreference value every time I start MainActivity.

hata
  • 11,633
  • 6
  • 46
  • 69
Jimmy
  • 1
  • 1
  • in your mainactivity's onCreate set the preference value to `0` again. – karan Apr 28 '20 at 12:48
  • I tried, but this is not working, after reaching the SharedPreferene getInt(); the value is changed and remains the same... – Jimmy Apr 28 '20 at 13:32

1 Answers1

0

You want to remove one key/value from shared preference here's how i do it.

public  void clearSpecificKey(String key){

    sharedPreferences.edit().remove(key).apply();
}

Few things to note : You should create a generic class of Shared Preference Like below

public class SharedPrefs {

private static final String MY_PREFS_NAME = "YOUR-PREFERENCE-NAME";

private static SharedPreferences sharedPreferences;
private String masterKeyAlias;
public SharedPrefs(Context context) {

    {
        try {
            masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
        } catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
        }
    }
    try {
        sharedPreferences = EncryptedSharedPreferences.create(MY_PREFS_NAME,masterKeyAlias,context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

}

public  String getStrings(Context mContext, String key){
    return sharedPreferences.getString(key, null);
}

public  void putString(Context mContext, String key, String value ){
    sharedPreferences.edit().putString(key, value).apply();
}

public  boolean getBoolean(Context mContext, String key){
    return sharedPreferences.getBoolean(key, false);
}

public  void putBoolean(Context mContext, String key, boolean value ){
    sharedPreferences.edit().putBoolean(key, value).apply();
}


public static void clear(Context mContext){
  sharedPreferences.edit().remove("user").apply();
}

public  void clearSpecificKey(String key){
    sharedPreferences.edit().remove(key).apply();

}

}

Here how to use it

Declaration :

SharedPrefs sharedPrefs;

Initialization :

sharedPrefs = new SharedPrefs(context);

just call the methods you want to use to store value in shared preference like

sharedPrefs.putString(context,key,value)

masterKeyAlias is to secure my Shared preferences.

Add this your app gradle

implementation "androidx.security:security-crypto:1.0.0-beta01"

you can read more about it here Best Practices

Malik Saifullah
  • 522
  • 1
  • 6
  • 22