How to intercept `onActivityResult` in Compose if ...
# compose
w
How to intercept
onActivityResult
in Compose if
startActivityForResult
is triggered by an external SDK and we don't have direct access to it?
Unfortunately,
rememberLauncherForActivityResult
only allows for launching activities and doesn't provide the option to register for result notifications directly.
I also tried to use
ActivityResultRegistry.register()
with
LocalActivityResultRegistryOwner.current.activityResultRegistry
, but we cannot do this in Compose due to the application being in the wrong state
RESUMED
instead of
STARTED
java.lang.IllegalStateException: LifecycleOwner com.app.main.MainActivity@f90bdab is attempting to register while the current state is RESUMED. LifecycleOwners must call register before they are STARTED.
s
The impl of
rememberLauncherForActivityResult
looks to call register from a DisposableEffect https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity[…]rActivityResult&ss=androidx%2Fplatform%2Fframeworks%2Fsupport, do you do the same and get this error that you are describing?
i
If an external SDK is directly calling the deprecated
startActivityForResult
, the only way to get the result from that is from
onActivityResult
up at the activity (or fragment, depending on how the SDK works) layer. The Activity Result APIs will only send you a response if you
launch
them - they cannot intercept other activity result requests. You should ask that SDK to provide an Activity Result Contract, which would ensure it could be used in an activity, fragment, or composable
👍 1
114 Views