5e43c0cf99bbbcc9923513981a94e0afe725e3ce
[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         String keyPreference = this.getActivity().getString(
21                 R.string.weather_preferences_units_key);
22         Preference connectionPref = this.findPreference(keyPreference);
23         connectionPref.setSummary(this.getPreferenceManager()
24                 .getSharedPreferences().getString(keyPreference, ""));
25
26         keyPreference = this.getActivity().getString(
27                 R.string.weather_preferences_language_key);
28         connectionPref = this.findPreference(keyPreference);
29         connectionPref.setSummary(this.getPreferenceManager()
30                 .getSharedPreferences().getString(keyPreference, ""));
31     }
32
33     @Override
34     public void onResume() {
35         super.onResume();
36         this.getPreferenceManager().getSharedPreferences()
37         .registerOnSharedPreferenceChangeListener(this);
38
39     }
40
41     @Override
42     public void onPause() {
43         super.onPause();
44         this.getPreferenceManager().getSharedPreferences()
45         .unregisterOnSharedPreferenceChangeListener(this);
46     }
47
48     @Override
49     public void onSharedPreferenceChanged(
50             final SharedPreferences sharedPreferences, final String key) {
51         String keyValue = this.getActivity().getString(
52                 R.string.weather_preferences_units_key);
53
54         if (key.equals(keyValue)) {
55             final Preference connectionPref = this.findPreference(key);
56             connectionPref.setSummary(sharedPreferences.getString(key, ""));
57         }
58
59         keyValue = this.getActivity().getString(
60                 R.string.weather_preferences_language_key);
61         if (key.equals(keyValue)) {
62             final Preference connectionPref = this.findPreference(key);
63             connectionPref.setSummary(sharedPreferences.getString(key, ""));
64         }
65
66     }
67
68 }