1

I have an app in which I am storing some values in shared preference but when I inspect code then it give

SharedPreferences.edit() without a corresponding commit() or apply() call.

How to resolve this warning.

For Shared preference I am using code mentioned below:-

private SharedPreferences m_Preference;
private SharedPreferences.Editor m_Editor;

m_Preference = context.getSharedPreferences(CStaticVar.DEAL_DOWNLOADED,Context.MODE_PRIVATE);
m_Editor = m_Preference.edit();
                    m_Editor.putInt("dealDonloadedcount",0);
                    m_Editor.apply();
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Rakesh
  • 21
  • 5

5 Answers5

0

In order to make changes to shared preferences you need to call

commit()

method.

Example(from Android official documentation)

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

More in Android official documentation: https://developer.android.com/training/basics/data-storage/shared-preferences.html

Rafal
  • 292
  • 2
  • 10
0

Create a class PreferencesHelper.java

public class PreferencesHelper {

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public PreferencesHelper(Context context) {
        this.sharedPreferences = context.getSharedPreferences("MyPreference", 0);
        this.editor = sharedPreferences.edit();
    }

    public String GetPreferences(String key) {
        return sharedPreferences.getString(key, "");
    }

    public void SavePreferences(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }
}

Then you can use the below code in your activity

PreferencesHelper pref = new PreferencesHelper(YourActivity.this);
//To Save Preferences
pref.SavePreferences("key",value);
//To Get Preferences
pref.GetPreferences("key");
Mridul S Kumar
  • 326
  • 1
  • 4
  • 20
0

I created the PreferencesHelper class proposed by Mridul S Kumar here but, for some reason, the warning was still showing up. To ignore the message with @SuppressLint("CommitPrefEdits") was my last resort. That's why I dug a little deeper and I tried the approach mentioned in this post and found out that the message was gone.

So I realized that the solution is to call the apply() method inside the method where the editor instance variable is initialized, without using "this" (I don't recommend you use commit() because then Android will advise you, with another warning, to consider using apply() instead, on the grounds that "commit writes its data to persistent storage, whereas apply will handle it in the background").

I'm not sure why, but what I do know is that there is no need to use it in this case, according to this post. The variable editor is not a reference to be disambiguated.

Therefore, the following code should solve your problem:

public class PreferencesHelper {
      private SharedPreferences sharedPreferences;
      private SharedPreferences.Editor editor;

      public PreferencesHelper(Context context) {
           this.sharedPreferences = context.getSharedPreferences("MyPreference", 0);
           this.editor = sharedPreferences.edit();

           boolean firstTime = sharedPreferences.getBoolean("first", true);
           if (firstTime) {
               editor.putBoolean("first", false);
               editor.apply();
           }
      }

      public String GetPreferences(String key) {
           return sharedPreferences.getString(key, "");
      }

      public void SavePreferences(String key, String value) {
           editor.putString(key, value);
           editor.commit();
      }
   }
}

I'm using Android 3.1.4, by the way.

-1

because if you change in Shared Preference the You should commit() or apply(). otherwise no meaning of this. for removing warning click on yellow mark on that particular line and disable inspection.

D.J
  • 1,439
  • 1
  • 12
  • 23
-1

use

editor.commit();

in place of

 editor.apply();
Mahesh Gawhane
  • 348
  • 3
  • 20