Status Update
Comments
il...@google.com <il...@google.com> #2
vi...@airbnb.com <vi...@airbnb.com> #3
Great! Thanks a lot, I'll look for the live updates soon!
il...@google.com <il...@google.com> #4
Yeah, you should never assume that your LifecycleOwnerAmbient
is anything but a LifecycleOwner
. For example, if you setContent
within a Fragment, it will be the fragment's viewLifecycleOwner
. That's true whether you are using Navigation or not. It just happens to be that NavHost
provides a LifecycleOwner
that is scoped to each individual screen (this is one of the roles of NavBackStackEntry
as per
The OnBackPressedDispatcherOwner
is not tied to LifecycleOwner
- it is something implemented by the base ComponentActivity
class (that FragmentActivity
and AppCompatActivity
extend). That's why relying on the ContextAmbient
is the correct starting point - it skips an intermediate NavHost
, Fragment
, or other LifecycleOwner
s that are irrelevant to what you are actually looking for.
We'll use this bug to track adding better APIs around getting the OnBackPressedDispatcherOwner
/ handling the back button in a @Composable
.
vi...@airbnb.com <vi...@airbnb.com> #5
That you for sharing more context. This makes sense and clearly I wasn't using it the way it was intended to be used.
ap...@google.com <ap...@google.com> #6
Branch: androidx-main
commit d1b8cb23445394c42002b874cc470f2eef08776e
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Feb 02 14:58:30 2021
Add BackHandler for handle system back in compose
Added a composable function, BackHandler, that hooks up to the
appropriate OnBackPressedDispatcher and handles back presses.
RelNote: "You can now use BackHandler to handle system back from a
composable function."
Test: BackPressDispatcherOwnerTest and BackHandlerTest
Bug: 172154006
Change-Id: I58ed5891125e093f3425a514c82266e25a684384
M activity/activity-compose/api/current.txt
M activity/activity-compose/api/public_plus_experimental_current.txt
M activity/activity-compose/api/restricted_current.txt
M activity/activity-compose/build.gradle
M activity/activity-compose/samples/build.gradle
A activity/activity-compose/samples/src/main/java/androidx/activity/compose/samples/BackHandlerSample.kt
A activity/activity-compose/src/androidTest/java/androidx/activity/compose/BackHandlerTest.kt
A activity/activity-compose/src/androidTest/java/androidx/activity/compose/BackPressedDispatcherOwnerTest.kt
A activity/activity-compose/src/main/java/androidx/activity/compose/BackHandler.kt
il...@google.com <il...@google.com> #7
We've added a BackHandler
API to allow any Composable to handle the system back button. This will be available in the upcoming androidx.activity:activity-compose
artifact.
il...@google.com <il...@google.com> #8
FWIW, this didn't land in Activity Compose 1.3.0-alpha01
, but it is now available in 1.3.0-alpha02
Description
I am using this BackButtonHandler based on a gist that Adam had shared earlier. I just started trying to use the navigation library in my app but I would still need this handler for taking care of some non-navigation use case. I see this error when I try to use this BackButtonHandler on a screen that was routed through the NavHostController
this is the handler in question
I started this slack thread for more context and the following discussion -https://kotlinlang.slack.com/archives/CJLTWPH7S/p1604266607160300
tldr; Ian Lake suggested that having something along the lines of a OnBackPressedDispatcherOwnerAmbient would be very useful.