react-native icon indicating copy to clipboard operation
react-native copied to clipboard

How to use registerForActivityResult with TurboModules in react native's new architecture?

Open BraveEvidence opened this issue 2 years ago • 2 comments

Description

Firstly I am not talking about the old architecture and I have already gone through this

This method does not really works for new architecture given how the files are structured

registerForActivityResult can only be used in MainActivity. I need registerForActivityResult for my intent to pick images.

Here is the complete code sample

The issue is I can't call my MainActivity in my Module as the Module file is not part of the main android folder but a separate folder called RTNImagePicker in my case

I get below error if I try to refer MainActivity in my module

hEmqe zYrD9

I cannot add it as dependency on module as well as it results in circular dependency

DFFg1

React Native Version

0.71.0

Output of npx react-native info

error Unrecognized command "info". info Run "react-native --help" to see a list of all available commands.

Steps to reproduce

You can download the sample from here and try it out

Snack, code example, screenshot, or link to a repository

https://github.com/BraveEvidence/ReactNativeNewArchregisterForActivityResultTry

BraveEvidence avatar Mar 05 '23 14:03 BraveEvidence

:warning: Newer Version of React Native is Available!
:information_source: You are on a supported minor version, but it looks like there's a newer patch available. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

github-actions[bot] avatar Mar 05 '23 14:03 github-actions[bot]

I already upgraded to 0.71.3 and still face the issue

BraveEvidence avatar Mar 05 '23 14:03 BraveEvidence

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jan 31 '24 05:01 github-actions[bot]

Answering here as I haven't got the time to get back to it.

As per Android documentation, registerForActivityResult can be called only during the Activity/Fragment creation. So this will have to happen inside the MainActivity (you'll have to instruct your users to do an invocation of a class of your library like this:

class MainActivity : ReactActivity() {
  
+ override fun onCreate(savedInstanceState: Bundle?) {
+   super.onCreate(savedInstanceState)
+   // Initialize my library here.
+   MyLibrary.intialize(this)
+ }

You can then call registerForActivityResult inside your library code, and retain the ActivityResultLauncher that gets returned, and invoke it inside your module whenever needed.

cortinico avatar Feb 07 '24 15:02 cortinico

Also this is unchanged between old and new architecture

cortinico avatar Feb 07 '24 15:02 cortinico