From bd6f255335988efb4deea3e5e8cc6f6bfcab3edc Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Sun, 18 Feb 2018 22:53:07 +0100 Subject: [PATCH] Recreate activity when perferences are changed --- .../health/openscale/gui/MainActivity.java | 39 +++++++++++++++++-- .../gui/activities/SettingsActivity.java | 26 ++++++++++++- .../gui/preferences/GeneralPreferences.java | 10 ----- .../app/src/main/res/values-cs/strings.xml | 3 +- .../app/src/main/res/values-de/strings.xml | 1 - .../app/src/main/res/values-es/strings.xml | 1 - .../app/src/main/res/values-sv/strings.xml | 1 - .../app/src/main/res/values/strings.xml | 1 - 8 files changed, 61 insertions(+), 21 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index 2c5b5d31..b3579b30 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -64,7 +64,8 @@ import java.lang.reflect.Field; import cat.ereza.customactivityoncrash.config.CaocConfig; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity + implements SharedPreferences.OnSharedPreferenceChangeListener{ private static boolean firstAppStart = true; private static boolean valueOfCountModified = false; private static int bluetoothStatusIcon = R.drawable.ic_bluetooth_disabled; @@ -79,7 +80,9 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { - String app_theme = PreferenceManager.getDefaultSharedPreferences(this).getString("app_theme", "Light"); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + + String app_theme = prefs.getString("app_theme", "Light"); if (app_theme.equals("Dark")) { setTheme(R.style.AppTheme_Dark); @@ -137,8 +140,6 @@ public class MainActivity extends AppCompatActivity { // Setup drawer view setupDrawerContent(navDrawer); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - selectDrawerItem(prefs.getInt("lastFragmentId", R.id.nav_overview)); navBottomDrawer.setSelectedItemId(prefs.getInt("lastFragmentId", R.id.nav_overview)); @@ -182,6 +183,35 @@ public class MainActivity extends AppCompatActivity { } } + private void registerOnSharedPreferenceChangeListener() { + PreferenceManager.getDefaultSharedPreferences(this) + .registerOnSharedPreferenceChangeListener(this); + } + + private void unregisterOnSharedPreferenceChangeListener() { + PreferenceManager.getDefaultSharedPreferences(this) + .unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onResume() { + super.onResume(); + // Stop listening when returning from settings + unregisterOnSharedPreferenceChangeListener(); + } + + @Override + public void onDestroy() { + // Clean up when shutting down + unregisterOnSharedPreferenceChangeListener(); + super.onDestroy(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + recreate(); + } + private void positiveFeedbackDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); @@ -277,6 +307,7 @@ public class MainActivity extends AppCompatActivity { prefs.edit().putInt("lastFragmentId", menuItemId).commit(); break; case R.id.nav_settings: + registerOnSharedPreferenceChangeListener(); Intent settingsIntent = new Intent(this, SettingsActivity.class); settingsIntent.putExtra(SettingsActivity.EXTRA_TINT_COLOR, navDrawer.getItemTextColor().getDefaultColor()); startActivity(settingsIntent); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/SettingsActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/SettingsActivity.java index 43d7a05b..623311d4 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/SettingsActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/SettingsActivity.java @@ -16,6 +16,7 @@ package com.health.openscale.gui.activities; import android.app.Fragment; +import android.content.SharedPreferences; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -30,7 +31,8 @@ import com.health.openscale.gui.utils.PermissionHelper; import java.util.ArrayList; import java.util.List; -public class SettingsActivity extends PreferenceActivity { +public class SettingsActivity extends PreferenceActivity + implements SharedPreferences.OnSharedPreferenceChangeListener { public static String EXTRA_TINT_COLOR = "tintColor"; private static List fragments = new ArrayList(); private Fragment currentFragment; @@ -46,6 +48,28 @@ public class SettingsActivity extends PreferenceActivity { super.onCreate(savedInstanceState); } + @Override + public void onResume() { + super.onResume(); + PreferenceManager.getDefaultSharedPreferences(this) + .registerOnSharedPreferenceChangeListener(this); + + } + + @Override + public void onPause() { + PreferenceManager.getDefaultSharedPreferences(this) + .unregisterOnSharedPreferenceChangeListener(this); + super.onPause(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (key.equals("app_theme")) { + recreate(); + } + } + @Override public void onBuildHeaders(List
target) { loadHeadersFromResource(R.xml.header_preferences, target); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/GeneralPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/GeneralPreferences.java index 6ca949fd..1875f4a3 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/GeneralPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/GeneralPreferences.java @@ -44,16 +44,6 @@ public class GeneralPreferences extends PreferenceFragment implements SharedPref appThemeList = (ListPreference)findPreference(PREFERENCE_KEY_APP_THEME); - appThemeList.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object o) { - Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.info_app_restart_required), Toast.LENGTH_LONG).show(); - - return true; - } - }); - - initSummary(getPreferenceScreen()); } diff --git a/android_app/app/src/main/res/values-cs/strings.xml b/android_app/app/src/main/res/values-cs/strings.xml index 089a6c72..10f612cf 100644 --- a/android_app/app/src/main/res/values-cs/strings.xml +++ b/android_app/app/src/main/res/values-cs/strings.xml @@ -40,7 +40,7 @@ %d den %d dny %d dnů - + Uplynulých 7 dnů Uplynulých 30 dnů Rozdíl hmotnosti @@ -118,7 +118,6 @@ automaticky Téma vzhledu - Aby se změny projevily, je třeba aplikaci restartovat Připomínání Dny v týdnu diff --git a/android_app/app/src/main/res/values-de/strings.xml b/android_app/app/src/main/res/values-de/strings.xml index 6e2cc4bd..96f2c5e4 100644 --- a/android_app/app/src/main/res/values-de/strings.xml +++ b/android_app/app/src/main/res/values-de/strings.xml @@ -180,7 +180,6 @@ Fehlerinformationen Allgmein Theme - Um die Änderungen zu übernehmen, ist ein App Neustart erforderlich Teilen Suche beendet nach Bluetooth Waagen Hilfe diff --git a/android_app/app/src/main/res/values-es/strings.xml b/android_app/app/src/main/res/values-es/strings.xml index 53fedb48..a9726fdc 100644 --- a/android_app/app/src/main/res/values-es/strings.xml +++ b/android_app/app/src/main/res/values-es/strings.xml @@ -197,7 +197,6 @@ Finalizada la búsqueda de básculas Bluetooth Tema - Para aplicar los cambios se requiere un reinicio de la aplicación Ayuda diff --git a/android_app/app/src/main/res/values-sv/strings.xml b/android_app/app/src/main/res/values-sv/strings.xml index abf05fe9..43cc0b18 100644 --- a/android_app/app/src/main/res/values-sv/strings.xml +++ b/android_app/app/src/main/res/values-sv/strings.xml @@ -164,7 +164,6 @@ Allmänt Fel: namnet måste vara minst 3 tecken Tema - En app-omstart krävs för att applicera ändringarna Uppskattar du openScale? Vad sägs om ett betyg på GooglePlay eller på GitHub? Skulle du ha något emot att ge oss lite återkoppling? diff --git a/android_app/app/src/main/res/values/strings.xml b/android_app/app/src/main/res/values/strings.xml index 4b2e84b1..f8b51019 100644 --- a/android_app/app/src/main/res/values/strings.xml +++ b/android_app/app/src/main/res/values/strings.xml @@ -155,7 +155,6 @@ auto Theme - To apply changes an app restart is required Reminder Weekdays