skip to Main Content

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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)
    }
    
    Login or Signup to reply.
  3. you can try this code as well,

    import androidx.activity.result.ActivityResult;
    import androidx.activity.result.ActivityResultCallback;
    import androidx.activity.result.ActivityResultRegistry;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.facebook.react.modules.core.PermissionAwareActivity;
    
    import javax.inject.Inject;
    
    public class MainActivity extends AppCompatActivity implements PermissionAwareActivity {
    
        @Inject
        ActivityResultRegistry mActivityRegistry;
    
        private ActivityResultCallback<ActivityResult> activityResultCallback = new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                // Handle the result of the activity here
            }
        };
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Obtain the ActivityResultRegistry instance from TurboModules
            TurboModuleRegistry.getEnforcedResolver(this).getJavaScriptContextHolder();
            TurboModuleRegistry.getEnforcedResolver(this).get(ActivityStarter.class).registerActivityResultListener(
                    new ActivityStarter.ActivityResultListener() {
                        @Override
                        public void onActivityResult(int requestCode, int resultCode, Intent data) {
                            // Pass the result to the registered ActivityResultCallback
                            activityResultCallback.onActivityResult(new ActivityResult(resultCode, data));
                        }
                    });
    
            // Use the ActivityResultRegistry to register the activity result callback
            mActivityRegistry.register("my_activity_result", this, activityResultCallback);
        }
    
        @Override
        public int checkSelfPermission(String permission) {
        }
    
        @Override
        public int checkPermission(String permission, int pid, int uid) {
        }
    
        @Override
        public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
        }
    
        @Override
        public boolean shouldShowRequestPermissionRationale(String permission) {
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        }
    }
    

    basically, with the PermissionAwareActivity interface it allows the activity to handle runtime permissions in a React Native application.I think its work for your scenario

    #edited 1.

    onActivityResult is deprecated,in place that you can use this as well registerForActivityResult

    ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            // Handle the result of the activity here
            // and send the callback to the TurboModule
        }
    );
    
    TurboModuleRegistry.getEnforcedResolver(this)
        .get(ActivityStarter.class)
        .registerActivityResultListener((requestCode, resultCode, data) -> {
            // Launch the activity using the activityResultLauncher
            activityResultLauncher.launch(data);
        });
    

    #edited 2- this code example for how we call this with our turbo module,

    public void startMyActivityForResult(ActivityResultLauncher<Intent> launcher) {
        ActivityStarter activityStarter = TurboModuleRegistry.get(ActivityStarter.class);
        activityStarter.launchActivityForResult(intent, launcher);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search