From fef9b4b9c2f78458371ad2f563f57d2ce6a719ad Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Fri, 4 Sep 2020 15:32:06 -0400 Subject: [PATCH 1/2] Fixes #639 - Unregister shared preference change listener so that we aren't leaving callbacks to a destroyed activity Make fields in SettingsActivity/SettingsFragment private --- .../andotp/Activities/SettingsActivity.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java index bb8e45e5..6353906c 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java @@ -66,10 +66,12 @@ import static org.shadowice.flocke.andotp.Utilities.Constants.EncryptionType; public class SettingsActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener{ - SettingsFragment fragment; - SecretKey encryptionKey = null; - boolean encryptionChanged = false; + private SettingsFragment fragment; + private SharedPreferences prefs; + + private SecretKey encryptionKey = null; + private boolean encryptionChanged = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -104,8 +106,8 @@ public class SettingsActivity extends BaseActivity .replace(R.id.container_content, fragment) .commit(); - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - sharedPref.registerOnSharedPreferenceChangeListener(this); + prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); } @Override @@ -277,18 +279,26 @@ public class SettingsActivity extends BaseActivity } } + @Override + protected void onDestroy() { + prefs.unregisterOnSharedPreferenceChangeListener(this); + prefs = null; + + super.onDestroy(); + } + public static class SettingsFragment extends PreferenceFragment { - PreferenceCategory catSecurity; + private PreferenceCategory catSecurity; - Settings settings; - ListPreference encryption; - Preference backupLocation; - ListPreference useAutoBackup; - CheckBoxPreference useAndroidSync; + private Settings settings; + private ListPreference encryption; + private Preference backupLocation; + private ListPreference useAutoBackup; + private CheckBoxPreference useAndroidSync; - OpenPgpAppPreference pgpProvider; - EditTextPreference pgpEncryptionKey; - OpenPgpKeyPreference pgpSigningKey; + private OpenPgpAppPreference pgpProvider; + private EditTextPreference pgpEncryptionKey; + private OpenPgpKeyPreference pgpSigningKey; public void encryptionChangeWithDialog(final EncryptionType encryptionType) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); From 28be98ea3897fd02473cae68410e274fee06d48c Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Fri, 4 Sep 2020 15:38:30 -0400 Subject: [PATCH 2/2] #639 - Unregister MainActivity shared pref listener so that we aren't leaving callbacks to a destroyed activity --- .../shadowice/flocke/andotp/Activities/MainActivity.java | 6 ++++++ .../org/shadowice/flocke/andotp/Utilities/Settings.java | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java index f913b44d..d378860f 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java @@ -888,4 +888,10 @@ public class MainActivity extends BaseActivity return super.onKeyDown(keyCode, event); } + + @Override + protected void onDestroy() { + settings.unregisterPreferenceChangeListener(this); + super.onDestroy(); + } } \ No newline at end of file diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java index 917e7b7e..961666d9 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java @@ -200,13 +200,13 @@ public class Settings { PreferenceManager.setDefaultValues(context, R.xml.preferences, true); } - - public void registerPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { settings.registerOnSharedPreferenceChangeListener(listener); } - + public void unregisterPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { + settings.unregisterOnSharedPreferenceChangeListener(listener); + } public boolean getTapToReveal() { return getTapSingle() == Constants.TapMode.REVEAL || getTapDouble() == Constants.TapMode.REVEAL;