Fixed
Status Update
Comments
lp...@google.com <lp...@google.com>
il...@google.com <il...@google.com> #2
Please include a sample project that reproduces your issue.
ch...@gmail.com <ch...@gmail.com> #3
While create sample, found dependence bug from Hilt.
@AndroidEntryPoint // Bug
class BugTwoPanelFragment : PreferenceHeaderFragmentCompat()
// OK, if without @AndroidEntryPoint
class FixTwoPanelFragment : PreferenceHeaderFragmentCompat()
@AndroidEntryPoint // OK
class HiltFixTwoPanelFragment : FixPreferenceHeaderFragmentCompat() // requireActivity().onBackPressedDispatcher.addCallback
il...@google.com <il...@google.com> #4
Thanks, that's exactly the information we needed.
ap...@google.com <ap...@google.com> #5
Project: platform/frameworks/support
Branch: androidx-main
commit b3acf3d80e5470fee45ad731fe4bfce7e469a238
Author: Ian Lake <ilake@google.com>
Date: Tue Jun 13 23:39:16 2023
Fix two pane preferences when using Hilt / Dialogs
Casting the Context to an OnBackPressedDispatcherOwner
fails in two particular cases:
- When using a library like Hilt which overrides
getContext() in a Fragment to wrap the Context in a
ContextWrapper
- When used in a Dialog, where we actually want to use
the OnBackPressedDispatcher of the ComponentDialog itself,
rather than using the Activity at all (which won't work in
a Dialog)
By using the findViewTreeOnBackPressedDispatcher() API,
we avoid using the Context at all and fix both of these cases.
Relnote: "`PreferenceHeaderFragmentCompat` now correctly handles
the system back button when used within a `ComponentDialog` or
when using libraries like Hilt's `@AndroidEntryPoint` that wrap the
Fragment's `Context`."
Relnote: "Preference now depends on Activity 1.5.1."
Test: tested in updated sample app
BUG: 286899046
Change-Id: Ie5d229c9494d37ad7cceb5b412987d70bcee5e3f
M preference/preference/build.gradle
M preference/preference/src/main/java/androidx/preference/PreferenceHeaderFragmentCompat.kt
M samples/SupportPreferenceDemos/src/main/java/com/example/androidx/preference/TwoPanePreferences.kt
A samples/SupportPreferenceDemos/src/main/res/layout/two_pane_preferences.xml
https://android-review.googlesource.com/2626430
Branch: androidx-main
commit b3acf3d80e5470fee45ad731fe4bfce7e469a238
Author: Ian Lake <ilake@google.com>
Date: Tue Jun 13 23:39:16 2023
Fix two pane preferences when using Hilt / Dialogs
Casting the Context to an OnBackPressedDispatcherOwner
fails in two particular cases:
- When using a library like Hilt which overrides
getContext() in a Fragment to wrap the Context in a
ContextWrapper
- When used in a Dialog, where we actually want to use
the OnBackPressedDispatcher of the ComponentDialog itself,
rather than using the Activity at all (which won't work in
a Dialog)
By using the findViewTreeOnBackPressedDispatcher() API,
we avoid using the Context at all and fix both of these cases.
Relnote: "`PreferenceHeaderFragmentCompat` now correctly handles
the system back button when used within a `ComponentDialog` or
when using libraries like Hilt's `@AndroidEntryPoint` that wrap the
Fragment's `Context`."
Relnote: "Preference now depends on Activity 1.5.1."
Test: tested in updated sample app
BUG: 286899046
Change-Id: Ie5d229c9494d37ad7cceb5b412987d70bcee5e3f
M preference/preference/build.gradle
M preference/preference/src/main/java/androidx/preference/PreferenceHeaderFragmentCompat.kt
M samples/SupportPreferenceDemos/src/main/java/com/example/androidx/preference/TwoPanePreferences.kt
A samples/SupportPreferenceDemos/src/main/res/layout/two_pane_preferences.xml
il...@google.com <il...@google.com> #6
This has been fixed internally and will be available in the next Preference release.
The actual fix is to use val onBackPressedDispatcherOwner = view.findViewTreeOnBackPressedDispatcherOwner()
, which fixes both this Hilt case (where they wrap the Context) and other cases where the Activity is not the correct OnBackPressedDispatcherOwner
such as in dialogs, Service
controlled views (which can also host fragments), etc.
Description
Component used: androidx.preference
Version used: 1.2.0
Android versions reproduced on: 12
Issue
Let's we have three fragments:
Main
,Settings
andSubSettings
:from NavigationComponent
Main
toSettings
vianavController.navigate(MainDirections.toSettings())
from 1.2.0 androidx.preference
Settings
toSubSettings
viaPreferenceHeaderFragmentCompat
fromNow we opened
SubSettings
and pressed back button. The result is pop toMain
, but was expectedSettings
(in vertical orientation).Fix
PreferenceHeaderFragmentCompat
hasonBackPressedCallback
, but it isn't work. BecauseonBackPressedDispatcher.addCallback
is unreachable.In onViewCreated owner always null.
I created custom
PreferenceHeaderFragmentCompat
where changedrequireContext()
torequireActivity()
and it's work as expected (fromSubSettings
toSettings
).