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

  • SEARCH
  • Home
  • 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 8200225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:22:08+00:00 2026-06-07T06:22:08+00:00

I have a Settings activity in my android application which purpose is to save

  • 0

I have a Settings activity in my android application which purpose is to save preferences upon an item selection. My problem is, my settings don’t get saved. The following method is called every time a user selection is made:

public void savePreferences()
{
    defaultVolumeUnit = MySingleton.getInstance().getDefaultVolumeUnit();
    defaultPressureUnit = MySingleton.getInstance().getDefaultPressureUnit();
    defaultTempUnit = MySingleton.getInstance().getDefaultTempUnit();

    settings = getSharedPreferences(SettingsTAG, 0);
    Editor editor = settings.edit();

    editor.putInt("selectVolume", defaultVolumeUnit);
    editor.putInt("selectPressure", defaultPressureUnit);
    editor.putInt("selectTemperature", defaultTempUnit);

    editor.commit();
}

I also use the following code in my MAIN activity where all the settings get restored upon start up:

public void restoreValues()
{
    settings = getSharedPreferences(SettingsTAG, 0);

    int SelectedVolume = settings.getInt("selectVolume", 0);
    int SelectedPressure = settings.getInt("selectPressure", 0);
    int SelectedTemperature = settings.getInt("selectTemperature", 0);

    // Necessary assignments here...
}

I use global variables throughout my whole application and those get saved but the settings do not. I’m positive that both savePreferences() and restoreValues() method gets called but whatever the user had selected doesn’t get saved.

In other words, nothing gets saved/restored. I don’t know what I’m doing wrong but this issue has been driving me nuts. This used to work for me before but I’m doing a Settings UI revamp and the same code suddenly isn’t working…

Any help please?

  • 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-06-07T06:22:11+00:00Added an answer on June 7, 2026 at 6:22 am

    Think you should be doing this in your activity:

    private SharedPreferences prefSettings;
    
    public void restoreValues()
    {
        prefSettings = PreferenceManager.getDefaultSharedPreferences(this);
    
        int SelectedVolume = prefSettings.getInt("selectVolume", 0);
        int SelectedPressure = prefSettings.getInt("selectPressure", 0);
        int SelectedTemperature = prefSettings.getInt("selectTemperature", 0);
    
        // Necessary assignments here...
    }
    

    In your method that I’ve noticed, your parameters do not look right actually…why are you using SettingsTAG, with 0 as parameters to the getSharedPreference method? Is your activity extending PreferenceActivity? You should be using the context.

    public void savePreferences()
    {
        defaultVolumeUnit = MySingleton.getInstance().getDefaultVolumeUnit();
        defaultPressureUnit = MySingleton.getInstance().getDefaultPressureUnit();
        defaultTempUnit = MySingleton.getInstance().getDefaultTempUnit();
    
        //settings = getSharedPreferences(SettingsTAG, 0);
        settings = PreferenceManager.getDefaultSharedPreferences(context); // Notice CONTEXT!
        Editor editor = settings.edit();
    
        editor.putInt("selectVolume", defaultVolumeUnit);
        editor.putInt("selectPressure", defaultPressureUnit);
        editor.putInt("selectTemperature", defaultTempUnit);
    
        editor.commit();
    }
    

    Edit:
    If you want to debug it, it might be better to do it this way:

    public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
        private static final String TAG = "PreferencesActivity";
    
    ///.... code....
    
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
        Log.d(TAG, "onSharedPreferenceChanged() - key = " + key);
        // Yup! The key within the shared preferences was changed... inspect 'em via Log!
    }
    

    The reason: you’re “listening” on the changes whenever its made to confirm that you are indeed saving the preferences. When you’re done with debugging, be sure to take out the Log calls so that it does not appear in the logcat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do wallpaper to android and have problem. When I click settings button I
I am working on an android application in which i have implemented voice recognition
Newbee to Android development. I am developing a application, which have three different tabs.
I know that Android doesn't have an Application-level onPause the way an Activity has
I have an application which uses the tts engine in Android, now as the
Is there a way to have an global settings variable for an android application,
This is the Android Live Wallpaper. .LiveWallpaperSettings is main activity with settings preferences. <-
I have settings page in my wp7 application. I loads for quite long time
I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different
I have some settings in my app.config which I intend to be 'global' -

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.