1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-30 11:40:23 +02:00

Recreate activity when perferences are changed

This commit is contained in:
Erik Johansson
2018-02-18 22:53:07 +01:00
parent ecc416e9b8
commit bd6f255335
8 changed files with 61 additions and 21 deletions

View File

@@ -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);

View File

@@ -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<String> fragments = new ArrayList<String>();
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<Header> target) {
loadHeadersFromResource(R.xml.header_preferences, target);

View File

@@ -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());
}

View File

@@ -40,7 +40,7 @@
<item quantity="one">%d den</item>
<item quantity="few">%d dny</item>
<item quantity="other">%d dnů</item>
</plurals>
</plurals>
<string name="label_last_week">Uplynulých 7 dnů</string>
<string name="label_last_month">Uplynulých 30 dnů</string>
<string name="label_weight_difference">Rozdíl hmotnosti</string>
@@ -118,7 +118,6 @@
<string name="label_automatic">automaticky</string>
<string name="label_theme">Téma vzhledu</string>
<string name="info_app_restart_required">Aby se změny projevily, je třeba aplikaci restartovat</string>
<string name="label_reminder">Připomínání</string>
<string name="label_reminder_weekdays">Dny v týdnu</string>

View File

@@ -180,7 +180,6 @@
<string name="customactivityoncrash_error_activity_error_details_clipboard_label">Fehlerinformationen</string>
<string name="title_general">Allgmein</string>
<string name="label_theme">Theme</string>
<string name="info_app_restart_required">Um die Änderungen zu übernehmen, ist ein App Neustart erforderlich</string>
<string name="label_share">Teilen</string>
<string name="label_bluetooth_searching_finished">Suche beendet nach Bluetooth Waagen</string>
<string name="label_help">Hilfe</string>

View File

@@ -197,7 +197,6 @@
<string name="label_bluetooth_searching_finished">Finalizada la búsqueda de básculas Bluetooth</string>
<string name="label_theme">Tema</string>
<string name="info_app_restart_required">Para aplicar los cambios se requiere un reinicio de la aplicación</string>
<string name="label_help">Ayuda</string>

View File

@@ -164,7 +164,6 @@
<string name="title_general">Allmänt</string>
<string name="error_user_name_too_short">Fel: namnet måste vara minst 3 tecken</string>
<string name="label_theme">Tema</string>
<string name="info_app_restart_required">En app-omstart krävs för att applicera ändringarna</string>
<string name="label_feedback_message_enjoying">Uppskattar du openScale?</string>
<string name="label_feedback_message_rate_app">Vad sägs om ett betyg på GooglePlay eller på GitHub?</string>
<string name="label_feedback_message_issue">Skulle du ha något emot att ge oss lite återkoppling?</string>

View File

@@ -155,7 +155,6 @@
<string name="label_automatic">auto</string>
<string name="label_theme">Theme</string>
<string name="info_app_restart_required">To apply changes an app restart is required</string>
<string name="label_reminder">Reminder</string>
<string name="label_reminder_weekdays">Weekdays</string>