Skip to content

Commit

Permalink
Support onActivityResult in Bridgeless (facebook#43351)
Browse files Browse the repository at this point in the history
Summary:

Implement `onActivityResult` on Bridgeless

Changelog:
[Internal] internal

Reviewed By: cortinico

Differential Revision: D54574139
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Mar 6, 2024
1 parent 3ed0ff3 commit dc8c80c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public abstract interface class com/facebook/react/ReactHost {
public abstract fun getJsEngineResolutionAlgorithm ()Lcom/facebook/react/JSEngineResolutionAlgorithm;
public abstract fun getLifecycleState ()Lcom/facebook/react/common/LifecycleState;
public abstract fun getReactQueueConfiguration ()Lcom/facebook/react/bridge/queue/ReactQueueConfiguration;
public abstract fun onActivityResult (Landroid/app/Activity;IILandroid/content/Intent;)V
public abstract fun onBackPressed ()Z
public abstract fun onHostDestroy ()V
public abstract fun onHostDestroy (Landroid/app/Activity;)V
Expand Down Expand Up @@ -3645,6 +3646,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
public fun getLifecycleState ()Lcom/facebook/react/common/LifecycleState;
public fun getMemoryPressureRouter ()Lcom/facebook/react/MemoryPressureRouter;
public fun getReactQueueConfiguration ()Lcom/facebook/react/bridge/queue/ReactQueueConfiguration;
public fun onActivityResult (Landroid/app/Activity;IILandroid/content/Intent;)V
public fun onBackPressed ()Z
public fun onHostDestroy ()V
public fun onHostDestroy (Landroid/app/Activity;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public boolean onBackPressed() {
public void onActivityResult(
int requestCode, int resultCode, Intent data, boolean shouldForwardToReactInstance) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
// TODO T156475655: Implement onActivityResult for Bridgeless
mReactHost.onActivityResult(mActivity, requestCode, resultCode, data);
} else {
if (getReactNativeHost().hasInstance() && shouldForwardToReactInstance) {
getReactNativeHost()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.facebook.react

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.queue.ReactQueueConfiguration
Expand Down Expand Up @@ -111,6 +112,14 @@ public interface ReactHost {
*/
public fun destroy(reason: String, ex: Exception?): TaskInterface<Void>

/* To be called when the host activity receives an activity result. */
public fun onActivityResult(
activity: Activity,
requestCode: Int,
resultCode: Int,
data: Intent?,
)

public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)

public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -603,6 +604,37 @@ RuntimeExecutor getRuntimeExecutor() {
return null;
}

/**
* To be called when the host activity receives an activity result.
*
* @param activity The host activity
*/
@ThreadConfined(UI)
@Override
public void onActivityResult(
Activity activity, int requestCode, int resultCode, @Nullable Intent data) {
final String method =
"onActivityResult(activity = \""
+ activity
+ "\", requestCode = \""
+ requestCode
+ "\", resultCode = \""
+ resultCode
+ "\", data = \""
+ data
+ "\")";
log(method);

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.onActivityResult(activity, requestCode, resultCode, data);
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Tried to access onActivityResult while context is not ready"));
}

@Nullable
JavaScriptContextHolder getJavaScriptContextHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
Expand Down

0 comments on commit dc8c80c

Please sign in to comment.