General settings are not saved after application restart

I found all the answers here and tried all the solutions, but my general privileges are not permanent.

Here is my code:

public static void setActivated(boolean activated) { SharedPreferences sp = Utils.getContext().getSharedPreferences( USER_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putBoolean(ASD, activated); editor.commit(); } public static boolean isActivated() { SharedPreferences sp = Utils.getContext().getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE); return sp.getBoolean(ASD, true); } 

I also tried:

 editor.clear(); editor.put .. editor.commit(); 

I also tried using

 editor.apply(); 

I even tried with .apply () and .commit () and no luck.

Another idea was to try using a different mode for the files:

 ...getSharedPreferences(USER_PREFS, Context.MODE_MULTI_PROCESS); 

The problem is that the stored values โ€‹โ€‹are not constant. If I close the application and then open it again, all values โ€‹โ€‹will be incorrect.

Does anyone have any ideas? I would also mention that the problem is only in some devices, for example, HTC One S, Samsung Galaxy S3 (I tested another S3 and worked fine).

EDIT: I cause the button clicks to be saved on the listener, and I call isActivated when the fragment loads (after onViewCreated ()).

Thanks!

+6
source share
3 answers
 public abstract SharedPreferences.Editor clear() 

Added to API Level 1 Check in the editor to remove all values โ€‹โ€‹from the preference. After calling commit, the only remaining preferences will be any that you defined in this editor. Note that when returning to preferences, clarity is first made, regardless of whether you called this editor clear before or after the put method.

Returns Returns a link to the same Editor object, so you can chain calls together.

In my user preference class, I was getting a null value for some other lines, and my code was something like this:

 SharedPreferences sp = Utils.getContext() .getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (session != null && !"".equals(session)) { sessionId = session; editor.putString(SESSION, sessionId).commit(); } else { sessionId = null; editor.clear().commit(); } 

editor.clear() dropped all my other commits!

+2
source

Hi, I think this should work. If cleaning does not work, you can try the second option as described in my solution:

You have 2 options:

  • Get overall preference over the activity lifecycle.

  • Call .clear to .commit

See my answer:

Android Persistent Checkable Menu in Custom Widget after Android reboot

+1
source

I don't know why, but it works by simply placing your prefs code inside the async task:

 prefss = getSharedPreferences(ACCOUNT_PREFS_NAME, MODE_MULTI_PROCESS); new AsyncSave(favNamesList).execute(); 
 private static class AsyncSave extends AsyncTask<Void, Void, Boolean> { String favNamesList; AsyncSave(String favNamesList) { this.favNamesList = favNamesList; } @Override protected Boolean doInBackground(Void... params) { prefss.edit().putString("favNamesList", strings).apply(); return null; } } 
0
source

Source: https://habr.com/ru/post/970995/


All Articles