d48747cb9c7962452e7ad1b8e1a06f6257d9bd17
[JavaForFun] /
1 package de.example.exampletdd.fragment;
2
3 import android.content.SharedPreferences;
4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
5 import android.os.Bundle;
6 import android.preference.Preference;
7 import android.preference.PreferenceFragment;
8 import de.example.exampletdd.R;
9
10 public class WeatherInformationPreferencesFragment extends PreferenceFragment
11 implements OnSharedPreferenceChangeListener {
12
13     @Override
14     public void onCreate(final Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16
17         // Load the preferences from an XML resource
18         this.addPreferencesFromResource(R.xml.weather_preferences);
19
20         final String unitsKey = this.getResources().getString(
21                 R.string.weather_preferences_units_key);
22         final Preference connectionPref = this.findPreference(unitsKey);
23         this.getPreferenceManager().getSharedPreferences()
24                 .getString(unitsKey, "");
25         connectionPref.setSummary(this.getPreferenceManager()
26                 .getSharedPreferences().getString(unitsKey, ""));
27     }
28
29     @Override
30     public void onResume() {
31         super.onResume();
32         this.getPreferenceManager().getSharedPreferences()
33         .registerOnSharedPreferenceChangeListener(this);
34
35     }
36
37     @Override
38     public void onPause() {
39         super.onPause();
40         this.getPreferenceManager().getSharedPreferences()
41         .unregisterOnSharedPreferenceChangeListener(this);
42     }
43
44     @Override
45     public void onSharedPreferenceChanged(
46             final SharedPreferences sharedPreferences, final String key) {
47         final String unitsKey = this.getResources().getString(
48                 R.string.weather_preferences_units_key);
49
50         if (key.equals(unitsKey)) {
51             final Preference connectionPref = this.findPreference(key);
52             connectionPref.setSummary(sharedPreferences.getString(key, ""));
53         }
54
55     }
56
57 }