The preferenceChangeListener
(Fig. 5.23) is an anonymous-inner-class object that implements the OnSharedPreferenceChangeListener
interface. This object was registered in method onCreate
to listen for changes to the app’s SharedPreferences
. When a change occurs, method onSharedPreferenceChanged
sets preferencesChanged
to true
(line 120), then gets a reference to the QuizFragment
(lines 122–123) so that the quiz can be reset with the new preferences. If the CHOICES
preference changed, lines 127–128 call the QuizFragment
’s updateGuessRows
and resetQuiz
methods.
111 // listener for changes to the app's SharedPreferences
112 private OnSharedPreferenceChangeListener preferenceChangeListener =
113 new OnSharedPreferenceChangeListener()
114 {
115 // called when the user changes the app's preferences
116 @Override
117 public void onSharedPreferenceChanged(
118 SharedPreferences sharedPreferences, String key)
119 {
120 preferencesChanged = true; // user changed app settings
121
122 QuizFragment quizFragment = (QuizFragment)
123 getFragmentManager().findFragmentById(R.id.quizFragment);
124
125 if (key.equals(CHOICES)) // # of choices to display changed
126 {
127 quizFragment.updateGuessRows(sharedPreferences);
128 quizFragment.resetQuiz();
129 }
130 else if (key.equals(REGIONS)) // regions to include changed
131 {
132 Set<String> regions =
133 sharedPreferences.getStringSet(REGIONS, null);
134
135 if (regions != null && regions.size() > 0)
136 {
137 quizFragment.updateRegions(sharedPreferences);
138 quizFragment.resetQuiz();
139 }
140 else // must select one region--set North America as default
141 {
142 SharedPreferences.Editor editor = sharedPreferences.edit();
143 regions.add(
144 getResources().getString(R.string.default_region));
145 editor.putStringSet(REGIONS, regions);
146 editor.commit();
147 Toast.makeText(MainActivity.this,
148 R.string.default_region_message,
149 Toast.LENGTH_SHORT).show();
150 }
151 }
152
153 Toast.makeText(MainActivity.this,
154 R.string.restarting_quiz, Toast.LENGTH_SHORT).show();
155 } // end method onSharedPreferenceChanged
156 }; // end anonymous inner class
157 } // end class MainActivity
If the REGIONS
preference changed, lines 132–133 get the Set<String>
containing the enabled regions. SharedPreferences
method getStringSet returns a Set<String>
for the specified key. The quiz must have at least one region enabled, so if the Set<String>
is not empty, lines 137–138 call the QuizFragment
’s updateRegions
and resetQuiz
methods. Otherwise, lines 142–146 update the REGIONS
preference with North America set as the default region, and lines 147–149 use a Toast
to indicate that the default region was set. Toast
method makeText receives as arguments the Context
on which the Toast
is displayed, the message to display and the duration for which the Toast
will be displayed. Toast
method show
displays the Toast
. Regardless of which preference changed, lines 153–154 display a Toast
indicating that the quiz will be reset with the new preferences. Figure 5.24 shows the Toast
that appears after the user changes the app’s preferences.