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 8743467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:40:56+00:00 2026-06-13T11:40:56+00:00

I’m trying to use SharedPreferences to store a preference, then change the default checked

  • 0

I’m trying to use SharedPreferences to store a preference, then change the default checked state of the menu item based on the SharedPreference. But it does not seem to be working. The menu choice stays until the app is closed. When I reload the app, the setting is back to the default, instead of the new SharedPreference setting.

public class MainActivity extends Activity {

boolean prefs = true;
String FILENAME = "settings";
String string;

public static final String LOG_TAG = "dbryant423";
public static final String PREFS_NAME = "settings";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(LOG_TAG, "prefs value: " +prefs);

 }

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference == "us")
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference == "metric")
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true; 
}

// called when an item is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) { // decide which MenuItem was pressed based on it's id
    case R.id.menu_us:
            menuUS();

    case R.id.menu_metric:
            menuMetric();

    }
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (prefs == true)
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (prefs == false)
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true;
}

public void menuUS() {
    prefs = true;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "us");
    editor.commit();
}

public void menuMetric() {
    prefs = false;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "metric");
    editor.commit();
}

public void calculateCylinder(View v) {
    Intent calculateCylinderUS = new Intent(this, CalculateCylinder.class);
    Intent calculateCylinderMetric = new Intent(this, CalculateCylinderMetric.class);
     Log.v(LOG_TAG, "prefs value: " +prefs);
    if (prefs == true)
        startActivity(calculateCylinderUS);
    else if (prefs == false)
        startActivity(calculateCylinderMetric);
}
  • 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-13T11:40:57+00:00Added an answer on June 13, 2026 at 11:40 am

    Please note that onPrepareOptionsMenu gets called just before the menu is displayed to the user.
    Looking at your code you are calling setChecked(true) on R.id.menu_us and R.id.menu_metric based on local variable prefs whose value will always be true whenever the activity is created. So in order to persist these states we must update menu-items based on preference values and not based on values of local variable.

    I think you’d rather modify your onPrepareOptionsMenuand onCreateOptionsMenu functions as below and give it a try:

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true; 
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String preference = settings.getString("preference", string);
        if (preference == "us")
            menu.findItem(R.id.menu_us).setChecked(true);
        else if (preference == "metric")
            menu.findItem(R.id.menu_metric).setChecked(true);
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.