I have a inner PreferenceScreen (call it Users) inside another PreferenceScreen (call it Main).
When i tap Users a new screen opens and I can change my Preferences there (a lot of CheckBoxes).
I want to detect (fire a callback) when this screen is dismissed and when I'm back on the Main PreferenceScreen.
The only way I found is to create a new class inheriting PreferenceScreen and overloading onPrepareForRemoval
I was wondering if there is a simpler way to do that.
Very interesting question! I finally figured out.
The trick is to set
DialogInterface.OnCancelListenerfor thePreferenceScreensubmenu (Users, in our case) and you can do it inonPreferenceTreeClick(as here internalDialogis already initialized). Sovoid onCancel(DialogInterface dialog)is the callback you've been looking for.Here's the
xml\preferences.xml:Here's the
PreferenceActivityandPreferenceFragmentI used:UPD: Solution for
PreferenceFragmentCompat:First, we need one more xml:
xml\subpreference.xml(duplicate of the submenu from the mainpreference.xml):Then, our hosting activity should
implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback. And last step - we need a new subfragment (or pass exact XML you need to inflate as a bundle's parameter):It this case, you can just listen to normal
onBackPressed()of ActivityI hope, it helps