Status Update
Comments
an...@google.com <an...@google.com>
il...@google.com <il...@google.com> #2
It is expected that BackHandler
crashes in cases where no LocalOnBackPressedDispatcherOwner
Given that LocalOnBackPressedDispatcherOwner.current
is nullable, you can certainly wrap your call to BackHandler
in a null check if you'd like to specifically disable that functionality when in @Preview
. Of course, you could just provide your own OnBackPressedDispatcherOwner
by constructing an OnBackPressedDispatcher
instance yourself as part of your @Preview
.
Andrey - any thoughts on whether we should have @Preview
automatically create and set a LocalOnBackPressedDispatcherOwner
as we do for LocalViewModelStoreOwner
an...@google.com <an...@google.com> #3
Note that in previews we do not really support ViewModels in reality. We provide a fake ViewModelStoreOwner, but this implementation just crashes when you try to create a ViewModel. It was a Sergey's idea as usually ViewModels are doing some network/database requests and we don't really support it in the preview mode.
ap...@google.com <ap...@google.com> #4
Branch: androidx-main
commit 026203513c5a704abeaa34595e7a7e7ab06876f7
Author: Ian Lake <ilake@google.com>
Date: Mon Jun 14 11:47:14 2021
Add an ActivityResultRegistry to Compose Previews
An ActivityResultRegistry is provided by default
by ComponentActivity, but using APIs that depend on
it, like rememberLauncherForActivityResult() would
crash in @Preview. By adding a fake implementation,
developers can preview those Composables.
For cases where they call `launch()` (i.e., the
`startActivityForResult()` equivalent), the fake
throws an IllegalStateException to indicate that
actually starting an activity / permission request
in Preview is not supported.
Relnote: "Compose `@Preview` now provides a
`LocalActivityResultRegistryOwner` that allows you
to preview Composables that use APIs like
`rememberLauncherForActivityResult()` that depend
on that owner existing."
Test: new ComposeViewAdapterTest test passes
BUG: 185693006
Change-Id: Ib13d12f085065adb89cfb731179b9295029e06e9
M compose/ui/ui-tooling/src/androidAndroidTest/kotlin/androidx/compose/ui/tooling/SimpleComposablePreview.kt
M compose/ui/ui-tooling/src/androidAndroidTest/kotlin/androidx/compose/ui/tooling/preview/ComposeViewAdapterTest.kt
M compose/ui/ui-tooling/src/androidMain/kotlin/androidx/compose/ui/tooling/preview/ComposeViewAdapter.kt
ap...@google.com <ap...@google.com> #5
Branch: androidx-main
commit 622947a991ba924d4896965f708be022bf1b35ba
Author: Ian Lake <ilake@google.com>
Date: Mon Jun 14 11:34:13 2021
Add a fake OnBackPressedDispatcher to Compose Previews
Rather than having APIs that depend on
LocalOnBackPressedDispatcher crash in @Preview,
provide a fake implementation by default to mirror
the capabilities that are available by default
in a ComponentActivity.
Relnote: "Compose `@Preview` now provides a
`LocalOnBackPressedDispatcherOwner` that allows you
to preview Composables that use APIs like `BackHandler`
that depend on that owner existing."
Test: new ComposeViewAdapterTest test passes
BUG: 185693006
Change-Id: Ia1c05ea3cadf2fb55cef9c4b8bae0898cfb35ba9
M compose/ui/ui-tooling/src/androidAndroidTest/kotlin/androidx/compose/ui/tooling/SimpleComposablePreview.kt
M compose/ui/ui-tooling/src/androidAndroidTest/kotlin/androidx/compose/ui/tooling/preview/ComposeViewAdapterTest.kt
M compose/ui/ui-tooling/src/androidMain/kotlin/androidx/compose/ui/tooling/preview/ComposeViewAdapter.kt
il...@google.com <il...@google.com> #6
We've added default a LocalOnBackPressedDispatcherOwner
and LocalActivityResultRegistryOwner
to @Preview
, so APIs that depend on these existing won't crash out of the box.
za...@gmail.com <za...@gmail.com> #7
Thanks!
Description
Steps to Reproduce:
BackHandler
composable.@Preview
composable.The preview will throw an exception about there not being any BackDispatcher available.
Because there isn't any back button in the IDE preview, and most previewed composables probably don't care about navigation anyway, I would expect
BackHandler
to just silently no-op in this case.