You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to use registerForActivityResult instead of onActivityResult #33639
We are using GitHub issues exclusively to track bugs in React Native. GitHub may not be the ideal place to ask a question, but you can try asking over on Stack Overflow, or on Reactiflux.
Is it impossible to use registerForActivityResult in react native module? Because you must extend ComponentActivity class and the only class which does so is ReactActivity.
But if you are creating a react native module, you cannot access ReactActivity, the best you can do is to get Activity from the method getCurrentActivity in ReactContextBaseJavaModule.
In my module I can't use startActivityForResult and need to use registerForActivityResult, is there any way to do it?
It depends on how your code is structured but something like:
// From ReactContextBaseJavaModuleval currentActivity = reactContext.currentActivity
if (currentActivity isAppCompatActivity) {
currentActivity.registerForActivityResult()
}
@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.
Code example:
val currentActivity = reactContext.currentActivity
if (currentActivity isAppCompatActivity) {
healthConnectManager =HealthConnectManager(reactContext, currentActivity)
}
healthConnectManager.authorize(promise)
classHealthConnectManager(
privatevalreactContext:ReactApplicationContext,
privatevalcurrentActivity:AppCompatActivity
) {
privateval healthConnectClient =HealthConnectClient.getOrCreate(reactContext)
privatevar permissions =HealthConnectPermissions.NONEprivateval myPluginScope =CoroutineScope(SupervisorJob() +Dispatchers.Main.immediate)
funauthorize(promise:Promise) {
myPluginScope.launch {
val grantedPermissions =
healthConnectClient.permissionController.getGrantedPermissions(PERMISSIONS)
if (grantedPermissions.containsAll(PERMISSIONS)) {
permissions =HealthConnectPermissions.ALL
promise.resolve(true)
} else {
Log.d(TAG, "Request permissions.")
requestPermissions.launch(PERMISSIONS)
}
}
}
// Create the permissions launcher.privateval requestPermissionActivityContract =
healthConnectClient.permissionController.createRequestPermissionActivityContract()
privateval 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
}
}
companionobject {
// build a set of permissions for required data typesprivatevalPERMISSIONS=setOf(
HealthPermission.createReadPermission(StepsRecord::class),
HealthPermission.createWritePermission(StepsRecord::class)
)
}
}
@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.
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.
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.
Okay, understood. So there is no way of implementing this from the module side, the user of the module would still need to add some code to MainActivity of his RN project.
Hello,
I use registerForActivityResult in my code in my React Native project. The activity launch is ok with this, but I never get the callback of registerForActivityResult executed.
Have you any ideas of what could be going wrong ?
@alarm109 did you ever get this to work, if so could you share a sample of how you did the permissions with registerForActivityResult. I'm currently working with the Health connect api and am looking for guidance on this specific issue :)
In my case the callback wasn't called, maybe because ReactActivity declares a onRequestPermissionsResult methods which probably gets called instead, the solution is to dispatch the result from here.
Override onRequestPermissionsResult in your MainActivity with
Activity
cortinico commentedon Jun 16, 2022
You can find documentation on
registerForActivityResulthere:https://developer.android.com/training/basics/intents/result
github-actions commentedon Jun 16, 2022
We are using GitHub issues exclusively to track bugs in React Native. GitHub may not be the ideal place to ask a question, but you can try asking over on Stack Overflow, or on Reactiflux.
BraveEvidence commentedon Jun 24, 2022
@cortinico but how to use this in a native module in react native. An example would be appreciated.
cortinico commentedon Jun 24, 2022
@PritishSawant have you checked the examples on https://developer.android.com/training/basics/intents/result#launch ?
BraveEvidence commentedon Aug 10, 2022
@cortinico yes but it is impossible to use this api given how react native is structured. An example would be appreciated.
alarm109 commentedon Sep 15, 2022
Is it impossible to use
registerForActivityResultin react native module? Because you must extendComponentActivityclass and the only class which does so isReactActivity.But if you are creating a react native module, you cannot access
ReactActivity, the best you can do is to getActivityfrom the methodgetCurrentActivityinReactContextBaseJavaModule.In my module I can't use
startActivityForResultand need to useregisterForActivityResult, is there any way to do it?cortinico commentedon Sep 21, 2022
It depends on how your code is structured but something like:
alarm109 commentedon Sep 23, 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.Code example:
cortinico commentedon Sep 26, 2022
The problem is self explanatory: you can't call
registerForActivityResultafter 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 therequestPermissionsas a field from the Activity.Legion2 commentedon 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.
alarm109 commentedon Sep 26, 2022
Okay, understood. So there is no way of implementing this from the module side, the user of the module would still need to add some code to
MainActivityof his RN project.@cortinico Thank you for the replies!
And last question, how do you get the
requestPermissionsas a field from the Activity inside your custom RN module?m-lce commentedon Oct 6, 2022
Hello,
I use registerForActivityResult in my code in my React Native project. The activity launch is ok with this, but I never get the callback of registerForActivityResult executed.
Have you any ideas of what could be going wrong ?
HilSny commentedon Oct 11, 2022
@alarm109 did you ever get this to work, if so could you share a sample of how you did the permissions with registerForActivityResult. I'm currently working with the Health connect api and am looking for guidance on this specific issue :)
yberstad commentedon Jan 17, 2023
Hi, this is how I set up the registerForActivityResult in my React Native project:
https://stackoverflow.com/a/75144449/3080858
serybva commentedon Feb 20, 2023
I may have a slightly better solution using the process described here https://developer.android.com/training/basics/intents/result#separate declaring the callback in the MainActivity isn't required anymore therefore the app stays decoupled from any react native module.
In my case the callback wasn't called, maybe because
ReactActivitydeclares aonRequestPermissionsResultmethods which probably gets called instead, the solution is to dispatch the result from here.Override
onRequestPermissionsResultin your MainActivity withAnd you're done
BraveEvidence commentedon Feb 21, 2023
Yes actually this is how i got it to work in flutter as well
BraveEvidence commentedon Feb 26, 2023
This can help https://www.youtube.com/watch?v=Bn-HIVb2ZIw
BraveEvidence commentedon Feb 28, 2023
Can someone help with how to use it with TurboModules https://stackoverflow.com/questions/75576907/how-to-use-registerforactivityresult-with-turbomodules-in-react-natives-new-arc
matinzd commentedon Aug 3, 2026
Let's see if this lands:
#57798