Android: SharedPreferences not getting updated from PreferenceFragment - java

I'm posting this question again as I didn't get any answers last time, and I still haven't solved the problem.
I have a settings menu with a PreferenceScreen in which I create a lot of CheckBoxPreferences during runtime (Adding them into the "Exclusion List" prefscreen). I created them no problem, here's the XML code below that it starts with:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:key="exclusion_list_key"
android:title="Exclusion List"
android:persistent="true"
android:summary="" >
</PreferenceScreen>
</PreferenceScreen>
I create the checkboxes in the onCreate method of my PreferenceFragment and add them to the "Exclusion List" PreferenceScreen and that works fine.
I'm trying to set the summary of the "Exclusion List" to be a list of all the checkbox titles that are checked off (so if the checkbox is checked, it's name will be listed in the summary of the "Exclusion List").
In the onCreate() method, the summary gets set properly, there's no problem.
But in the onPreferenceChanged() method, I set the summary of "Exclusion List" to the 'summary' string I built (which contains the correct value), but it doesn't update it! When i press back from my checkbox menu, the "Exclusion List" does not have the updated values.
The last few lines are the ones of interest. I did some printlns to see what's going on:
The listener works fine, runs when expected
My summary var contains what's expected
After calling setSummary(summary), the getSummary() returns the expected value (so that means it got set properly)
However, when I actually press back and see "Exclusion List", it's summary doesn't actually get updated!
Did I miss something? Thanks in advance!
All the code for reference:
public class Settings extends AppCompatActivity {
public static final String EXC_LIST_KEY = "exclusion_list_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
String summary = "";
//Create all the checkboxes inside of the PreferenceScreen
for (int i = 0; i < MainActivity.contactNames.length; i++) {
CheckBoxPreference checkbox = new CheckBoxPreference(getActivity());
checkbox.setTitle(MainActivity.contactNames[i][0]);
checkbox.setKey(MainActivity.contactNames[i][2]);
checkbox.setSummary(MainActivity.contactNames[i][1]);
checkbox.setDefaultValue(false);
((PreferenceScreen) findPreference(EXC_LIST_KEY)).addPreference(checkbox);
if (checkbox.isChecked()) {
summary = summary + checkbox.getTitle() + "\n";
}
}
findPreference(EXC_LIST_KEY).setSummary(summary);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//Checkbox keys all start with 'key_'
if (key.startsWith("key_")) {
String summary = "";
for (int i = 0; i < MainActivity.contactNames.length; i++) {
CheckBoxPreference checkbox = (CheckBoxPreference) findPreference(MainActivity.contactNames[i][2]);
if (checkbox.isChecked()) {
summary = summary + checkbox.getTitle() + "\n";
}
}
System.out.println("Summary I have: " + summary); //Correct summary is printed out
findPreference(EXC_LIST_KEY).setSummary(summary); //Isn't updating the value???
System.out.println("Summary system has: " + findPreference(EXC_LIST_KEY).getSummary()); //Correct summary is printed out
}
}
}
}

try Adding this code in your SettingsFragment
#Override
public void onResume() {
super.onResume();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
// CHANGE 1: load saved values to set the summaries
onSharedPreferenceChanged(prefs, "exclusion_list_key");
// CHANGE 2: register shared prefs listener in onResume
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}

Got it working by adding the following line of code to the end of my onSharedPreferenceChanged() method:
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();

Related

"onRadioButtonClicked" method not working for radio button implemented by "onCreate" method

I tried to populate RadioGroup's RadioButtons on "onCreateMethod" rather than using XML because my purpose is to get it from some sort of database or other business objects model that works with randomicity. RadioButtons are fine, but nothing happens when I click them otherwise when I created in XML activity file, not a log message neither a test toast. By the way, as I said, I need to create the buttons by code, thanks, is my first steps in Android.
My Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/quiz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
My Code:
public class ListaAlunosActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
LinearLayout questoesQuiz = (LinearLayout) findViewById(R.id.quiz);
// Log.d(TAG,"Populate List View; Displaying Data in the List View");
ArrayList<String> dataList = new ArrayList<>(Arrays.asList("sup1", "sup2", "sup3"));
RadioGroup listaDeQuestoes = new RadioGroup(this);
listaDeQuestoes.setOrientation(RadioGroup.VERTICAL);
RadioGroup.LayoutParams lp;
for (int i = 0; i < dataList.size(); i++) {
RadioButton botao = new RadioButton(this);
botao.setText(dataList.get(i));
lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT);
listaDeQuestoes.addView(botao, lp);
}
questoesQuiz.addView(listaDeQuestoes);
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
final String TAG = "MyActivity";
Log.v("On clicked working", "clicado");
int id = view.getId();
Toast toast2 = Toast.makeText(this, "toast working", Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast2.show();
}
}
Buttons are okay!
using onClick for finding selected radio button is not the best solution but because you want to use onClick i will show you how to do it with minimum changes to your code. make these three changes to your code:
public class ListaAlunosActivity extends AppCompatActivity
implements View.OnClickListener {// <------ 1. implement OnClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
LinearLayout questoesQuiz = (LinearLayout) findViewById(R.id.quiz);
// Log.d(TAG,"Populate List View; Displaying Data in the List View");
ArrayList<String> dataList = new ArrayList<>(Arrays.asList("sup1", "sup2", "sup3"));
RadioGroup listaDeQuestoes = new RadioGroup(this);
listaDeQuestoes.setOrientation(RadioGroup.VERTICAL);
RadioGroup.LayoutParams lp;
for (int i = 0; i < dataList.size(); i++) {
RadioButton botao = new RadioButton(this);
botao.setOnClickListener(this);// <---------- 2.add this line
botao.setText(dataList.get(i));
lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT);
listaDeQuestoes.addView(botao, lp);
}
questoesQuiz.addView(listaDeQuestoes);
}
#Override
public abstract void onClick (View v){ //<-------- 3. override onClick
boolean checked = ((RadioButton) v).isChecked();
final String TAG = "MyActivity";
Log.v("On clicked working", "clicado");
int id = v.getId();// your radio buttons have no id thus use title instead of id:
String title = ((RadioButton) v).getText();
Toast toast2 = Toast.makeText(this, "toast working", Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast2.show();
}
If you look keenly, your onRadioButtonClicked is just a method that is never called. Now what you have to do is make the Activity implement RadioGroup.OnCheckedChangeListener. And in onCheckedChanged method, do the Toast and it will work. Here is the code.
public class ListaAlunosActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alnus);
LinearLayout questoesQuiz = (LinearLayout) findViewById(R.id.quiz);
// Log.d(TAG,"Populate List View; Displaying Data in the List View");
ArrayList<String> dataList = new ArrayList<>(Arrays.asList("sup1", "sup2", "sup3"));
RadioGroup listaDeQuestoes = new RadioGroup(this);
listaDeQuestoes.setOrientation(RadioGroup.VERTICAL);
RadioGroup.LayoutParams lp;
for (int i = 0; i < dataList.size(); i++) {
RadioButton botao = new RadioButton(this);
botao.setId(i);
botao.setText(dataList.get(i));
lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT);
listaDeQuestoes.addView(botao, lp);
}
questoesQuiz.addView(listaDeQuestoes);
listaDeQuestoes.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast toast2 = Toast.makeText(this, "toast working for id "+ checkedId, Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast2.show();
}
}
I did this and it works perfectly
Not completely sure what your code is meant to do, but I would use a setOnCheckedChangeListener rather than onClick.
Something like this
listaDeQuestoes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
final String TAG = "MyActivity"; // not used?
Log.v("On clicked working", "clicado");
// Note checkedId is +1 when accessing the arraylist so needs to be decremented to get a list item
Toast toast2 = Toast.makeText(ListaAlunosActivity.this, "toast working clicked (" + checkedId + ") [" + dataList.get(checkedId - 1) + "]", Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast2.show();
}
});
The Toast is working and I showed how you can determine which button was clicked and how to access your data array with it, if desired.
The main reason for this, as BAHMAN points out, is that you haven't set any listener. HOWEVER. Setting a listener on the buttons themselves is not a very good idea. It is better to set it on the radio group. And it's better to have your layout elements in your layout file. This makes them easier to modify and understand.
Another thing that is personal preference: I prefer implementing the listener as an anonymous class where it is set. The solutions where the class implements the listener make it harder to read for large classes where it can be annoying to go looking for listeners. I might make an exception if the listener is very complex or if it something that might be used more than once.
I also cleaned up the code a bit. Comments added where I did
Anyway, here's how I would write this code:
Main Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/quiz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="#+id/radio_button_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Main Activity Code:
public class ListaAlunosActivity extends AppCompatActivity {
// I put your tag at the top of the class so it's more useful
public static final String TAG = "ListaAlunosActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
// You didn't really need an arraylist here for this static content.
// I just made it an array
String[] dataList = {"sup1", "sup2", "sup3"};
// Get this from your layout instead of adding it manually.
// It's a cleaner way to set up the layout that makes the
// code more maintainable
RadioGroup listaDeQuestoes = findViewById(R.id.radio_button_list);
// I changed this to a for each loop because it's a little cleaner
for (String name : dataList){
RadioButton botao = new RadioButton(this);
botao.setText(name);
listaDeQuestoes.addView(botao);
}
// This is the code that will react to the new radio button being selected
listaDeQuestoes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Either way we do it, we need to grab the view to get the name
RadioButton buttonView = group.findViewById(checkedId);
// You can use this code to get the index if you need it
int checkedIndex = group.indexOfChild(buttonView);
// And you can use either of these methods to get the name:
String buttonNameFromView = buttonView.getText().toString();
String buttonNameFromDataSource = dataList[checkedIndex];
String output = "Button with Id: " + checkedId + " and Name: " + buttonNameFromView + " was clicked";
Log.v(TAG, output);
Toast toast = Toast.makeText(ListaAlunosActivity.this, output, Toast.LENGTH_LONG);
// I set gravity to just center here. This is the same as center_vertical | center_horizontal. Personally, I wouldn't set it at all.
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
}
}

Variable's value is different that the assigned one

I am using a fragment which shows differnet informations about an object. Inside onCreate of the fragment I retrieve some values from the object and store them in global variables:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
String serializedShow = getArguments().getString(SHOW_TO_LOAD);
mShow = (new Gson()).fromJson(serializedShow, Show.class);
mScope = mShow.getScope();
mLanguage = MainActivity.getSystemLanguage();
mLanguage = mLanguage.replace("_", "-");
mId = Integer.valueOf(mShow.getShowId()).toString(); //this line is important
Log.v(LOG_TAG, "DetailFragment onStart, Id is " + mId);
}
As you can see I assign to this variable a specific ID. I use the log to check if the value of mId is the correct one and it is. Everything works well so far.
Next int he code the user can click a button to upen up am url in the browser. The URls are different with each time this fragment is created (every time this fragment shows different stuff)
private void fetchIMDBId(){
Log.v(LOG_TAG, "starting IMDBAsyncTask with id " + mId);
new Async4().execute(mId);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_launch:
fetchIMDBId();
return true;
default:
break;
}
return false;
}
As you can see the method fetchIMDBId() gets called. I have a log line to check if the value of mId which was set before in onCreate is the same.
It is not the same, its a totally different value.
Specifically is the id of the object that was being displayed in a previous instance if this fragment.
Please tell me what I'm doing wrong. Thanks.
E: variable declaration
public class DetailFragment extends Fragment {
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
...
private String mScope, mLanguage, mId;
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
}
E: full source here
I believe you are not assigning your mId correctly to the Async task. You should apply the mid to the async task method so that the corresponding mId from each fragment is called
private void fetchIMDBId(String mId){
Log.v(LOG_TAG, "starting IMDBAsyncTask with id " + mId);
new Async4().execute(mId);
}
and then call the method within the fragment
fetchIMDBId(this.mId);
Try using this.mId and also check if your fragment object is same as previous one.
Call this.fetchIMDBId() instead.

Using SharedPreferences to store and retrieve int

This is driving me crazy. Cannot seem to get this to work at all. I'm pretty inexperienced so any tips would be grand. I hope it is just a stupid mistake that someone can explain.
A number is derived in one class. The number is then used within that class without a problem. Within that Class I store that number using the shared preferences method. However in a later Class when I attempt to retrieve that number the app crashes out.
This is the class where I process and try to store the number:
public class Summary extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summary);
int Time = gotTime();
// Create object of SharedPreferences.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Summary.this);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putInt("TargetTime", Time);
//commits your edits
editor.
// I then display the Time on that screen which all works ok
TextView textViewT = (TextView) findViewById(R.id.textView4);
textViewT.setText("Time is " + Time);
};
public int gotTime(){
//do stuff to generate the value for time stored as variable PenileTim
return PenileTim;
};
}
Then in a future class I try to retrieve the Time:
public class Map extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int VarTime = sharedPref.getInt("TargetTime", (Integer) null);
TextView textViewP = (TextView) findViewById(R.id.textTest);
textViewP.setText(" " + VarTime );
}
}
Anybody have any idea what is going wrong?

onSharedPreferenceChanged isn't called everytime a preference has been changed

I have a problem with onSharedPreferenceChanged is only called the first time when a MultiSelectListPreference has changed. I open my settings activity and change the value which works fine and onSharedPreferenceChanged is getting called. If I open the dialog again, it shows the correct entries selected. I select another entry and hit OK. onSharedPreferenceChanged should now getting called but isn't. If I now open the dialog again, no entries are selected. Am I missing something or did I do somethign wrong?
Here's my preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<MultiSelectListPreference
android:key="operations"
android:title="#string/pref_operations"
android:dialogTitle="#string/pref_operations"
android:entries="#array/pref_operations_entries"
android:entryValues="#array/pref_operations_values"
android:defaultValue="#array/pref_operations_default" />
</PreferenceScreen>
And my settings fragment
public class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener
{
public static final String KEY_OPERATIONS_PREFERENCE = "operations";
private MultiSelectListPreference operationsPreference;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
operationsPreference = (MultiSelectListPreference) getPreferenceScreen().findPreference(KEY_OPERATIONS_PREFERENCE);
}
#Override
public void onResume()
{
super.onResume();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Set<String> operations = operationsPreference.getValues();
String summary = "";
for (String s : operations)
summary += s + " ";
operationsPreference.setSummary(summary);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause()
{
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if (key.equals(KEY_OPERATIONS_PREFERENCE))
{
Set<String> operations = operationsPreference.getValues();
String summary = "";
for (String s : operations)
summary += s + " ";
operationsPreference.setSummary(summary);
}
}
}
Umm frankly I built my sharedPreferances differently...ill attach my code example and hopefully it'll be of use to you
here goes:
this is the sharedpreferences class
public class AppPreferences {
public static final String KEY_LANG = "language";
final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); // Name
// of
// the
// file
// -.xml
private SharedPreferences _sharedPrefs;
private Editor _prefsEditor;
public AppPreferences(Context context) {
this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS,
Activity.MODE_PRIVATE);
this._prefsEditor = _sharedPrefs.edit();
}
public String getlanguage() {
return _sharedPrefs.getString(KEY_LANG, "");
}
public void savelanguage(String text) {
_prefsEditor.putString(KEY_LANG, text);
_prefsEditor.commit();
}
}
to use it youll need to create
in your class
private AppPreferences _appPrefs;
and an example for use is
_appPrefs = new AppPreferences(getApplicationContext());
_appPrefs.savelanguage("English");
language = _appPrefs.getlanguage();
this is how i built it... works like a charm... and not complicated at all
It's because you unregister the listener in your onPause callback method which is invoked when Dialog is shown in an Activity.

CheckBoxPreference not ticking when selected in PreferencesActivity

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;
}
});
}
}
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".
Update the values from the shared preferences in onResume()
method
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(PrefsActivity.this);
boolean isReg = prefs.getBoolean("isReg", false);
Hope Try This it will help You

Categories