Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:02:51+00:00 2026-05-25T06:02:51+00:00

I am trying to use shared preferences to store my apps settings, but even

  • 0

I am trying to use shared preferences to store my apps settings, but even though i tried to copy tutorial best as i can i still can’t make it work. Here are parts of my class:

public class ActivitySettings extends PreferenceActivity {

@SuppressWarnings("unused")
private static String TAG = "ActivitySettings";

private static final String PREFS_NAME = "preferences";

private static final String DISABLE_CHECK = "disableCheck";
private static final String ALWAYS_CONFIRM = "alwaysConfirm";
private static final String NEVER_CONFIRM = "neverConfirm";
private static final String SHOW_NOTIFICATION = "showNotification";
private static final String SHOW_ON_BOOT = "showOnBoot";
private static final String HIDE_ICON = "hideIcon";
private static final String LOGGING = "logging";

private Context context = this;

private CheckBoxPreference disableCodeCheck;
private CheckBoxPreference alwaysAskForConf;
private CheckBoxPreference neverAskForConf;
private CheckBoxPreference showNotif;
private CheckBoxPreference showAtBoot;
private CheckBoxPreference hideIcon;
private Preference exportData;
private Preference importData;
private Preference cleanUp;
private Preference reset;
private CheckBoxPreference logging;

private boolean isDisableCodeCheck;
private boolean isAlwaysAskForConf;
private boolean isNeverAskForConf;
private boolean isShowNotif;
private boolean isShowAtBoot;
private boolean isHideIcon;
private boolean isLogging;

private void loadPreferences() {
    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);

    isDisableCodeCheck = prefs.getBoolean(DISABLE_CHECK, false);
    isAlwaysAskForConf = prefs.getBoolean(ALWAYS_CONFIRM, false);
    isNeverAskForConf = prefs.getBoolean(NEVER_CONFIRM, false);
    isShowNotif = prefs.getBoolean(SHOW_NOTIFICATION, false);
    isShowAtBoot = prefs.getBoolean(SHOW_ON_BOOT, false);
    isHideIcon = prefs.getBoolean(HIDE_ICON, false);
    isLogging = prefs.getBoolean(LOGGING, false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    disableCodeCheck = (CheckBoxPreference) findPreference("disable_code_check");
    alwaysAskForConf = (CheckBoxPreference) findPreference("always_ask_for_conf");
    neverAskForConf = (CheckBoxPreference) findPreference("never_ask_for_conf");
    showNotif = (CheckBoxPreference) findPreference("show_notif");
    showAtBoot = (CheckBoxPreference) findPreference("show_at_boot");
    hideIcon = (CheckBoxPreference) findPreference("hide_icon");
    exportData = findPreference("exp");
    importData = findPreference("imp");
    cleanUp = findPreference("clean_up");
    reset = findPreference("reset");
    logging = (CheckBoxPreference) findPreference("logging");

    loadPreferences();

    //here are also booleans modified

@Override
protected void onStop() {
    super.onStop();
    savePreferences();
}

private void savePreferences() {
    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);

    prefs.edit().putBoolean(DISABLE_CHECK, isDisableCodeCheck);
    prefs.edit().putBoolean(ALWAYS_CONFIRM, isAlwaysAskForConf);
    prefs.edit().putBoolean(NEVER_CONFIRM, isNeverAskForConf);
    prefs.edit().putBoolean(SHOW_NOTIFICATION, isShowNotif);
    prefs.edit().putBoolean(SHOW_ON_BOOT, isShowAtBoot);
    prefs.edit().putBoolean(HIDE_ICON, isHideIcon);
    prefs.edit().putBoolean(LOGGING, isLogging);

    prefs.edit().commit();
}

}

Here is what happens:

  1. there are two files created in shared_prefs: preferences.xml and packagename_preferences.xml. Don’t know why, PREFS_NAME is provided.

  2. loading settings is done in onCreate methos, saving and commiting in onStop

  3. using adb shell and cat i’m looking into files while app is running and here is the scenario:

    • i open settings and tick sth and packagename_preferences.xml is created with correct value
    • i press back and second file is created – preferences.xml which looks like this:
      <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
      <map />
    • when i open settings again everything looks like on first run and values in packagename_preferences.xml are changed to default(?)
  4. i know there is and built in mechanism for PreferenceActivity but i don’t want to use it because I need access to other settings from other activities.


EDIT


I have created such app:

package pl.test;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;

public class TestActivity extends Activity {
String s = "0";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SharedPreferences sp = getSharedPreferences("pref", 0);  
    s = sp.getString("setting2", "1");
    s = "5";
}

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

}

And it doesn’t work it just does not save string to pref.xml. What is wrong!?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:02:52+00:00Added an answer on May 25, 2026 at 6:02 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey so I am trying to use Shared Preferences to store information that is
Trying to use a guid as a resource id in a rest url but
I'm trying to permanently store three strings as user preferences for my Android application.
I am trying to use shared_ptr with my class but for some reason I
I am trying to figure out the best way to store my application key
I'm trying to use shared_ptr for the first time here, but I'm having some
I've tried to find a comprehensive guide on whether it is best to use
I'm trying to use OpenID on my site but I get this error: Protocol
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.