From 6cb5dfd3559109416174a4416498cc137cb73e5c Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Thu, 9 Jul 2020 15:43:32 +0200 Subject: [PATCH] Fix sub-screens in settings not opening the edit dialog --- .../preference/PreferenceManagerBinder.java | 13 ++++++++++++ .../settings/PreferenceCompatActivity.java | 21 ++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/androidx/preference/PreferenceManagerBinder.java diff --git a/app/src/main/java/androidx/preference/PreferenceManagerBinder.java b/app/src/main/java/androidx/preference/PreferenceManagerBinder.java new file mode 100644 index 00000000..63457113 --- /dev/null +++ b/app/src/main/java/androidx/preference/PreferenceManagerBinder.java @@ -0,0 +1,13 @@ +package androidx.preference; + +/** + * A helper to attach a new {@link PreferenceManager} to a {@link Preference}. This class is in the {@code androidx.preference} package to hack + * around the protected visibility of {@link Preference#onAttachedToHierarchy(PreferenceManager)}. + */ +public class PreferenceManagerBinder { + private PreferenceManagerBinder() {} + + public static void bind(Preference pref, PreferenceManager manager) { + pref.onAttachedToHierarchy(manager); + } +} diff --git a/app/src/main/java/org/transdroid/core/gui/settings/PreferenceCompatActivity.java b/app/src/main/java/org/transdroid/core/gui/settings/PreferenceCompatActivity.java index 627e20bc..b1b20e28 100644 --- a/app/src/main/java/org/transdroid/core/gui/settings/PreferenceCompatActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/settings/PreferenceCompatActivity.java @@ -8,19 +8,20 @@ import androidx.appcompat.app.AppCompatCallback; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceManagerBinder; import androidx.preference.PreferenceScreen; - public class PreferenceCompatActivity extends AppCompatActivity implements AppCompatCallback, PreferenceFragmentCompat.OnPreferenceStartScreenCallback { +public class PreferenceCompatActivity extends AppCompatActivity implements AppCompatCallback, PreferenceFragmentCompat.OnPreferenceStartScreenCallback { - private PreferenceFragmentCompat fragment; + private PreferenceFragmentCompat fragment; - public void addPreferencesFromResource(@XmlRes int preferencesResId) { - fragment = new RootPreferencesFragment(preferencesResId); - getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commitNow(); - } + public void addPreferencesFromResource(@XmlRes int preferencesResId) { + fragment = new RootPreferencesFragment(preferencesResId); + getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commitNow(); + } - public PreferenceManager getPreferenceManager() { - return fragment.getPreferenceManager(); + public PreferenceManager getPreferenceManager() { + return fragment.getPreferenceManager(); } public PreferenceScreen getPreferenceScreen() { @@ -66,6 +67,10 @@ import androidx.preference.PreferenceScreen; @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { if (prefs != null) { + // Update the already loaded preferences with this fragment's manager to handle dialog clicks, etc. + for (int i = 0; i < prefs.getPreferenceCount(); i++) { + PreferenceManagerBinder.bind(prefs.getPreference(i), getPreferenceManager()); + } setPreferenceScreen(prefs); prefs = null; }