This is what a helper looks like.

public class MyPrefs { public static final String PREFS_NAME = "MyPrefsFile"; private final String APPS = "apps"; private static MyPrefs instanse; private SharedPreferences mPref; public static MyPrefs getInstanse() { if (null != instanse) { return instanse; } return new MyPrefs(); } private MyPrefs() { mPref = MyApplication.getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); } //App's settings public void addApp(String appName){ HashSet<String> apps = getBootApps(); apps.add(appName); addAllBootApps(apps); } public void addAllApps(HashSet<String> apps){ SharedPreferences.Editor editor = mPref.edit(); editor.putStringSet(APPS, apps); editor.apply(); } public HashSet<String> getApps(){ return (HashSet<String>) mPref.getStringSet(APPS,new HashSet<String>()); } public void deleteApp(String appName){ HashSet<String> apps = getApps(); apps.remove(appName); addAllApps(apps); } public void removewAllApps(){ addAllApps(new HashSet<String>()); } } 

with it, I first write through addApp() and immediately getApps() contents with getApps() and all changes are visible, but after restarting the application there is nothing there, and I use the same methods.

    1 answer 1

    The issue was resolved, added the line editor.clear() to the method:

     public void addAllBootApps(HashSet<String> apps){ SharedPreferences.Editor editor = mPref.edit(); editor.clear(); editor.putStringSet(APPS, apps); editor.apply(); } 

    The reason for which everything earned me is still not clear. Here is the ref to the answer that helped me.

    PS If someone can explain why it worked, would be grateful!?

    • MB has accumulated a whole bunch of prefs? cleaning up past data helps you before putting a record in there) - iFr0z
    • Well, yes, there are still places in the code where I use SP, but there are only a couple of such places. - Kirill Stoianov