question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use registerForActivityResult instead of onActivityResult

See original GitHub issue

Description

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:closed
  • Created a year ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Legion2commented, Sep 26, 2022

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.

1reaction
cortinicocommented, Sep 26, 2022

@cortinico, I tried your example, but now I get this error: LifecycleOwner com.xxx.MainActivity@c3ccf41 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.

The problem is self explanatory: you can’t call registerForActivityResult after the activity has started.

I suggest you move the:

    private val requestPermissions =
        currentActivity.registerForActivityResult(requestPermissionActivityContract) { granted ->
            permissions = if (granted.containsAll(PERMISSIONS)) {
                Log.d(TAG, "All permissions granted")
                HealthConnectPermissions.ALL
            } else {
                Log.d(TAG, "Lack of permissions")
                HealthConnectPermissions.PARTIAL
            }
        }

inside your MainActivity and then just call healthConnectManager.authorize(promise) as you’re doing today. You’ll have to get the requestPermissions as a field from the Activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OnActivityResult method is deprecated, what is the alternative?
From now, startActivityForResult() has been deprecated so use new method instead of that. Kotlin Example
Read more >
StartActivityForResult is deprecated - Medium
Learn how to use RegisterForActivityResult with examples in Kotlin.
Read more >
Android Katha: onActivityResult is Deprecated. Now What?
We can now launch activities for the result from fragments and receive the results in fragments without going through the activity. Instead of ......
Read more >
Getting a result from an activity - Android Developers
Use Jetpack Compose on Wear OS · Compose performance · Navigation ... Overview · Active data and exercise · Passive data updates ·...
Read more >
StartActivityForResult is Deprecated! - MongoDB
Let's try to understand how to use the new API with a few examples. ... override fun onActivityResult(requestCode: Int, resultCode: Int, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found