React Native onActivityResult不起作用

时间:2016-03-31 13:54:01

标签: android webview react-native

我有一个React Native的webview组件。 webview应该支持输入类型是文件,所以我这样做:

File Upload in WebView

并且webview实现ActivityEventListener并覆盖onActivityResult.But onActivityResult不起作用。

守则

class RNWebView extends WebView implements ActivityEventListener {
      protected class GeoWebChromeClient extends WebChromeClient {
          public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
           ...
            mActivity.startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
            return true;
          }
      }

    public RNWebView(ReactContext reactContext, Activity activity) {
        super(reactContext);
        // Add the listener for `onActivityResult`
        reactContext.addActivityEventListener(this);
        ...
     }

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    // Your logic here
    Log.d("Tanck", "requestCode:" + requestCode + "----" + "resultCode:" + resultCode);
}

}

2 个答案:

答案 0 :(得分:11)

也许迟到希望它有所帮助。

本地模块的onActivityResultReactContext调用,由ReactInstanceManagerImpl调用,ReactActivity调用0.29。在上面的示例中,MyWb扩展了活动而不是ReactActivity,因此永远不会调用ReactInstanceManagerImpl

解决方案就在您的活动的onActivityResult调用ReactInstanceManager的{​​{1}}中,因为您已拥有自己的ReactInstanceManager对象引用。

您正在启动React Native的活动应该具有如下覆盖:

onActivityResult

答案 1 :(得分:1)

为了使解决方案完整:

在您的 MainActivity(或在 Activity 中,如果它是本机应用程序的一部分,则在其中初始化 RN 并使用 mReactInstanceManager):

...
private ReactInstanceManager mReactInstanceManager;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
}

在您的模块中:

public class MyModule extends ReactContextBaseJavaModule implements ActivityEventListener {
    static final int REQUEST_VIDEO_CAPTURE = 1;

    final ReactApplicationContext reactContext;
    Promise promise;

    public GeneralIntentModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.reactContext.addActivityEventListener(this);
    }

    @Override
    public String getName() {
        return "MyModule ";
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.promise.resolve(data.getDataString());
    }
}