2

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

enter image description here

enter image description here

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

enter image description here

BraveEvidence
  • 53
  • 11
  • 45
  • 119
  • In your case that circular dependency means your RTNImagePicker is importing MainActivity and MainActivity is importing RTNImagePicker? Why MainActivity is importing RTNImagePicker? – Jonas Beck Mar 02 '23 at 12:10
  • Because I need registerForActivityResult which is only available inside an Mainactivity or in class which has the Activity reference – BraveEvidence Mar 02 '23 at 14:34

2 Answers2

0

You will need to have a gateway module. A gateway module would act as a buffer between the two modules and take care of any dependency related issue

eg

interface MainActivityFinder {
    fun homeScreenIntent(context: Context): Intent
}

Now use this interface as means to pass the data from one activity to another using registerForActivityResult after adding this gateway module as a dependency in your project.

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • How do I launch the intent? A more detailed code sample would be useful. Also I am not using two activities. One is activity and the other is a module – BraveEvidence Feb 27 '23 at 05:48
0

Hope this helps you:

  • Creating a new interface where has 'pickImage' virtual function.
  • Let MainActivity implement the new interface and define 'pickImage' function entity.
  • In RTNImagePicker, importing the new interface instead of MainActivity.
  • Cast context as the new interface to call 'pickImage' function.

MainActivity:

interface NewInterface {
 func pickImage(i: Intent)
}
class MainActivity : ReactActivity(), NewInterface {
 ...
 override func pickImage(intent: Intent) {
  resultLauncher.launch(intent)
 }
 ...
}

RTNImagePicker:

override fun picImage(promise?: Promise) {
 ...
 val activity = context?.currentActivity as NewInterface
 activity.picImage(intent)
}
Jonas Beck
  • 246
  • 4
  • I will again face the cyclic dependency issue. MainActivity and RTNImagePicker are in different modules – BraveEvidence Mar 03 '23 at 00:02
  • I am not sure if you get me correct. The point is to create a new interface where has 'pickImage' function that will be called in RTNImagePicker module. RTNImagePicker module would import that new interface instead of MainActivity so by the result of this you could avoid to let MainActivity and RTNImagePicker import each other. – Jonas Beck Mar 03 '23 at 19:02
  • In which module should I have the interface. There is separate module for RTNImagePicker and MainActivity – BraveEvidence Mar 04 '23 at 03:22
  • Looking at your updated code it again results in cyclic dependency as the interface is in MainActivity's module and RTNImagePicker is in completely different module. I am not sure if you have worked with react native's new architecture but check this once https://reactnative.dev/docs/the-new-architecture/pillars-turbomodules – BraveEvidence Mar 04 '23 at 03:24