How to use registerForActivityResult instead of onActivityResult
See original GitHub issueDescription
I am not able to use registerForActivityResult in Android custom Native module. I am stuck with the deprecated onActivityResult
Version
0.68
Output of npx react-native info
System: OS: macOS 12.3.1 CPU: (8) x64 Apple M1 Memory: 50.59 MB / 8.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.13.0 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 8.1.0 - /usr/local/bin/npm Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5 Android SDK: Not Found IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8193401 Xcode: 13.3/13E113 - /usr/bin/xcodebuild Languages: Java: javac 16 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.67.4 => 0.67.4 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found
Steps to reproduce
Try creating a native module and replacing onActivityResult with registerForActivityResult
Snack, code example, screenshot, or link to a repository
No response
Issue Analytics
- State:
- Created a year ago
- Comments:13 (5 by maintainers)
Top GitHub Comments
We are developing a SDK which provides React Native UI via a Fragment. We create a single React Native Context for multiple fragments, so we can preload and share the JS runtime and native modules, because react native startup time is a major issue. But then there is no single place where we can call registerForActivityResult. We could call registerForActivityResult in the first fragment’s onCreate, but if the React Native Context is reused in a new Fragment later, the registered handler from the first Fragment becomes invalid and cannot be used anymore.
The problem is self explanatory: you can’t call
registerForActivityResult
after the activity has started.I suggest you move the:
inside your MainActivity and then just call
healthConnectManager.authorize(promise)
as you’re doing today. You’ll have to get therequestPermissions
as a field from the Activity.