Android M Weird Shared Preferences Issue

Android M weird shared preferences issue

That is because Android M will feature Automatic Backups (old link).

Extract:

The automatic backup feature preserves the data your app creates on a
user device by uploading it to the user’s Google Drive account and
encrypting it. There is no charge to you or the user for data storage
and the saved data does not count towards the user's personal Drive
quota. During the M Preview period, users can store up to 25MB per
Android app.

Weird problem with Shared Preferences in Android

Your problem is the fact that you are generating a new Editor object with every call to sp.edit(). So your call sp.edit().commit() is creating a new editor that has no changes to commit. Try this:

private void savePreferences() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(DISABLE_CHECK, isDisableCodeCheck);
editor.putBoolean(ALWAYS_CONFIRM, isAlwaysAskForConf);
editor.putBoolean(NEVER_CONFIRM, isNeverAskForConf);
editor.putBoolean(SHOW_NOTIFICATION, isShowNotif);
editor.putBoolean(SHOW_ON_BOOT, isShowAtBoot);
editor.putBoolean(HIDE_ICON, isHideIcon);
editor.putBoolean(LOGGING, isLogging);
editor.commit();
}

Alternatively, The editor methods are designed to be chained, so this would also work:

private void savePreferences() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0)
prefs.edit().putBoolean(DISABLE_CHECK, isDisableCodeCheck)
.putBoolean(ALWAYS_CONFIRM, isAlwaysAskForConf)
.putBoolean(NEVER_CONFIRM, isNeverAskForConf)
.putBoolean(SHOW_NOTIFICATION, isShowNotif)
.putBoolean(SHOW_ON_BOOT, isShowAtBoot)
.putBoolean(HIDE_ICON, isHideIcon)
.putBoolean(LOGGING, isLogging)
.commit();
}

You have the same problem in your test code, which can be fixed like so:

@Override
public void onStop() {
super.onStop();
SharedPreferences sp = getSharedPreferences("pref", 0);
sp.edit().putString("setting2", s).commit();
}

Android - Strange behavior shared preferences after API 29

I managed to find the following information:

  1. The xml declaration should be:

<androidx.preference.PreferenceScreen ... and <androidx.preference.SwitchPreference ...


  1. It seems that getDefaultSharedPreferences(Context context) is no longer supported (neither for AndroidX)

The problem I am facing is: How to use a preference screen without the default preference manager? There is no other way to read the data afaik.

Solution:

Disable AutoUpdate inside the manifest. This fixes the issue.

Strange unexplained behaviour in SharedPreferences Android

Well I finally found what went wrong after doing a proper search, check out the answer provided here. Note that this is a bug in android and happens to string sets only. Thanks
Set<String> in android sharedpreferences does not save on force close

Weird sharedPreference issue with my Android app

I had similar symptoms, only to later find out it was the autobackup feature (introduced with Android 6). Uninstalling doesn't clear that data, since it's fetched from Google's cloud (simply your Google Drive).

Read more: https://developer.android.com/guide/topics/data/autobackup

To test this simply disable the backup feature, and repeat the tests to make sure you get the expected default values. Set android:allowBackup to false:

<manifest ... >
...
<application android:allowBackup="false" ... >
...
</application>
</manifest>

Also, I can not but urge you not to store permission states on the local disk. Why not call Context.checkCallingOrSelfPermission() in runtime? it is the safer way.

Weird OutOfMemory on SharedPreferences Crash

Thanks for the replies, but I managed to make it work with the SharedPreferences.

I removed the variable to the start of class and modified methods, were as follows:

public void checkCurtains() {
Boolean check = false;

SharedPreferences mPrefs = this.getSharedPreferences("mPrefCurtains", MODE_PRIVATE);
check = mPrefs.contains("jsonCurtains");

setUpCurtains(check);
}

private void checkCarpets(){

Boolean check = false;

SharedPreferences mPrefs = this.getSharedPreferences("mPrefCarpets", MODE_PRIVATE);
check = mPrefs.contains("jsonCarpets");

setUpCarpets(check);
}

private void checkQuits(){
Boolean check = false;

SharedPreferences mPrefs = this.getSharedPreferences("mPrefQuits", MODE_PRIVATE);
check = mPrefs.contains("jsonQuits");

setUpQuits(check);
}

Thanks again!

Android issue with boolean on shared preferences

sorry i will not post the question correctly

The problem is not with preference it returns correct value the problem is in handler which is used to slide the viewpager based on the value which is returned by preference

the handler is running even though the activity is killed below the code solved my problem

SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSlide=spref.getBoolean("SLIDE_SHOW",false);

if(isSlide==true)
{
SlideShowTimer();
}

public void SlideShowTimer()
{
mFilterTask = new Runnable() {
@Override
public void run() {
int i=0;
itemPosition = viewPager.getCurrentItem() + 1;
if(itemPosition!=10)
{
if(i <= adapter.getCount()-1)
{
viewPager.setCurrentItem(itemPosition,true);
mHandler.postDelayed(mFilterTask, 3000);
i++;
}
}
else
{
//ViewPagerAdapter.mediaPlayer.stop();
ViewPagerActivity.this.finish();
}

}
};
mHandler.removeCallbacks(mFilterTask);
mHandler.postDelayed(mFilterTask,3000);
}
@Override
public void onDestroy(){
super.onDestroy();
mHandler.removeCallbacks(mFilterTask);
}

SharedPreferences issue in Android Studio

I think, I fixed it myself.
Multiple tests doesn't show any problem.
Please let me know if this could be done in a better way.

My
Settings Fragment class

public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sharedPreferences;

@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
addPreferencesFromResource(R.xml.root_preferences);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("sync")){
boolean on = sharedPreferences.getBoolean("sync", false);
if(on){
Toast.makeText(getActivity(), "Switch enabled", Toast.LENGTH_SHORT).show();
if(AudioPlay.mediaPlayer == null) { AudioPlay.playAudio(getContext(),R.raw.app_bg_music_orig); AudioPlay.mediaPlayer.setLooping(true);}
else {AudioPlay.resumeAudio();}
}
else {
Toast.makeText(getActivity(), "Switch disabled", Toast.LENGTH_SHORT).show();
if(AudioPlay.mediaPlayer == null) { Log.i("Switch position",""+on); }
else {AudioPlay.pauseAudio();}
}
}
}

@Override
public void onStart() {
super.onStart();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onStop() {
super.onStop();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}


My
Settings class

public class SettingsActivity extends AppCompatActivity {
SharedPreferences.OnSharedPreferenceChangeListener listener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSharedPreferences("sync",MODE_PRIVATE).registerOnSharedPreferenceChangeListener(listener);

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean my_bg_music = preferences.getBoolean("sync",true);
if(my_bg_music){
if(AudioPlay.mediaPlayer == null) { AudioPlay.playAudio(this,R.raw.app_bg_music_orig); AudioPlay.mediaPlayer.setLooping(true);}
else {AudioPlay.resumeAudio();}
}
else{
if(AudioPlay.mediaPlayer == null) { Log.i("Switch position",""+my_bg_music); }
else {AudioPlay.pauseAudio();}
}
}
@Override
public void onResume() {
getSharedPreferences("sync",MODE_PRIVATE).registerOnSharedPreferenceChangeListener(listener);
super.onResume();
}

@Override
public void onPause() {
getSharedPreferences("sync",MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(listener);
super.onPause();
}
}


Related Topics



Leave a reply



Submit