From 9d3d92cc9d98a908a5e2147aee0c6d5cb6b4db47 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 22 Apr 2018 21:26:13 +0200 Subject: [PATCH] Fixed crash when changing settings Looking at Fragment1, clicking Fragment2 Fragment2.onCreate Fragment2.onResume Fragment1.onPause <- This sets PreferenceUI to Fragment1, so onClickListeners in Fragment2 now have a reference to the wrong fragment --- .../activity/PreferenceActivity.java | 20 +++++++++---------- .../preferences/PreferenceController.java | 8 +++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/PreferenceActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/PreferenceActivity.java index 83578ff5c..a6aec2425 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/PreferenceActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/PreferenceActivity.java @@ -113,16 +113,18 @@ public class PreferenceActivity extends AppCompatActivity { } public static class MainFragment extends PreferenceFragment { + private int screen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); - addPreferencesFromResource(getArguments().getInt(PARAM_RESOURCE)); + screen = getArguments().getInt(PARAM_RESOURCE); + addPreferencesFromResource(screen); PreferenceActivity activity = instance.get(); if (activity != null && activity.preferenceController != null) { activity.preferenceUI.setFragment(this); - activity.preferenceController.onCreate(getArguments().getInt(PARAM_RESOURCE)); + activity.preferenceController.onCreate(screen); } } @@ -131,9 +133,9 @@ public class PreferenceActivity extends AppCompatActivity { super.onResume(); PreferenceActivity activity = instance.get(); if(activity != null && activity.preferenceController != null) { - activity.setTitle(getTitle(getArguments().getInt(PARAM_RESOURCE))); + activity.setTitle(getTitle(screen)); activity.preferenceUI.setFragment(this); - activity.preferenceController.onResume(getArguments().getInt(PARAM_RESOURCE)); + activity.preferenceController.onResume(screen); } } @@ -159,9 +161,8 @@ public class PreferenceActivity extends AppCompatActivity { @Override public void onPause() { PreferenceActivity activity = instance.get(); - if(activity != null && activity.preferenceController != null) { - activity.preferenceUI.setFragment(this); - activity.preferenceController.onPause(getArguments().getInt(PARAM_RESOURCE)); + if (screen == R.xml.preferences_integrations) { + activity.preferenceController.unregisterGpodnet(); } super.onPause(); } @@ -169,9 +170,8 @@ public class PreferenceActivity extends AppCompatActivity { @Override public void onStop() { PreferenceActivity activity = instance.get(); - if(activity != null && activity.preferenceController != null) { - activity.preferenceUI.setFragment(this); - activity.preferenceController.onStop(); + if (screen == R.xml.preferences_storage) { + activity.preferenceController.unsubscribeExportSubscription(); } super.onStop(); } diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index b2d1fa4d2..fceaf6abf 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -638,13 +638,11 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc } } - public void onPause(int screen) { - if (screen == R.xml.preferences_integrations) { - GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener); - } + public void unregisterGpodnet() { + GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener); } - public void onStop() { + public void unsubscribeExportSubscription() { if (subscription != null) { subscription.unsubscribe(); }