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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:34:37+00:00 2026-06-12T17:34:37+00:00

When I click the Settings page, my CheckBoxPreference shows exactly how I want it

  • 0

When I click the “Settings” page, my CheckBoxPreference shows exactly how I want it to at this stage, however when I select it, the state does not change (i.e. from Unchecked to Checked).

It seems to be retaining the value of false (default) and still running onPreferenceChanged with false as the value passed. I am running code inside my onPreferenceChangedListener that is dependent on the value of the preference that is changed. As it stands I only have one preference in here.

activity_preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceScreen>
        <CheckBoxPreference 
            android:summaryOn="true"
            android:summaryOff="false"
            android:key="isReg"
            android:title="Receive Push Messages"
            android:selectable="true"
            android:enabled="true"
            android:persistent="true" />
    </PreferenceScreen>
</PreferenceScreen>

PrefsActivity.java:

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

import com.google.android.gcm.GCMRegistrar;

public class PrefsActivity extends PreferenceActivity{

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);       
    addPreferencesFromResource(R.xml.activity_preferences);

    Preference isReg = (Preference) findPreference("isReg");
    isReg.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

        public boolean onPreferenceChange(Preference pref, Object arg1) {
            boolean isReg = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isReg", false);
            if (isReg){
                ServerUtilities.register(getApplicationContext(), GCMRegistrar.getRegistrationId(getApplicationContext()));
            }
            else {
                ServerUtilities.unregister(getApplicationContext(), GCMRegistrar.getRegistrationId(getApplicationContext()));
            }
            return isReg;
        }

    });
}
}
  • 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-12T17:34:38+00:00Added an answer on June 12, 2026 at 5:34 pm

    You are using the wrong preference widget as per in the code sample,

    Preference isReg = (Preference) findPreference("isReg");
    

    It should have been CheckBoxPreference as in

    CheckBoxPreference isReg = (CheckBoxPreference) findPreference("isReg");
    

    Edit
    One thing I noticed, you’re not handling the preference activity properly…consider this code, always, call the preference’s commit method in order to save it!

    public class PrefsActivity extends PreferenceActivity implements
                                       OnSharedPreferenceChangeListener{
    CheckBoxPreference isReg;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.activity_preferences);
        PreferenceManager.setDefaultValues(this,R.xml.activity_preferences, false);
        isReg = (CheckBoxPreference)findPreference("isReg");
        if (isReg != null){
                isReg.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
                    @Override
                    public boolean onPreferenceChange(Preference preference,
                            Object newValue) {
                        boolean blnIsReg = Boolean.getBoolean(newValue.toString());
                        Editor e = _prefs.edit();
                        e.putBoolean("isReg", blnIsReg);
                        e.commit();
                        return true;
                    }
                });
            }
        }
    }
    
    @Override
    public void onStart(){
        super.onStart();
        _prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }
    

    The listener that is implemented must be in this fashion, which is how the changes are effective:

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
         Log.d(TAG, "onSharedPreferenceChanged; key = " + key);
    }
    

    By incorporating the listener, the logcat will show that the preference’s key is indeed being changed, i.e. “isReg”.

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

Sidebar

Related Questions

To change Proxy server I have to go to IE->Internet Options->LAN Settings and click
When I click the Settings|Document Library Settings menu the page that appears for my
I've got a user page, where user can change his settings ( users#edit ).
First let me say that this is not about production settings for django -
I have User-settings page in my ASP.Net MVC3 application. This page contains a lot
I am just creating a settings page where a user can change his user
I'm writing a user account page where users can change their settings, and the
This is my custom page in Active Admin ActiveAdmin.register_page Settings do action_item do link_to('Import
When I click Settings in my application's menu, app crash instantly. I don't have
Basically, I have a settings window, and when you click OK, it's suppose to

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.