1 package de.example.exampletdd.fragment;
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;
10 public class WeatherInformationPreferencesFragment extends PreferenceFragment
11 implements OnSharedPreferenceChangeListener {
14 public void onCreate(final Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
17 // Load the preferences from an XML resource
18 this.addPreferencesFromResource(R.xml.weather_preferences);
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, ""));
30 public void onResume() {
32 this.getPreferenceManager().getSharedPreferences()
33 .registerOnSharedPreferenceChangeListener(this);
38 public void onPause() {
40 this.getPreferenceManager().getSharedPreferences()
41 .unregisterOnSharedPreferenceChangeListener(this);
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);
50 if (key.equals(unitsKey)) {
51 final Preference connectionPref = this.findPreference(key);
52 connectionPref.setSummary(sharedPreferences.getString(key, ""));