diff --git a/docs/NativeModulesAndroid.md b/docs/NativeModulesAndroid.md index 121d1e767..a53f5a122 100644 --- a/docs/NativeModulesAndroid.md +++ b/docs/NativeModulesAndroid.md @@ -344,7 +344,7 @@ public void onActivityResult( We will implement a simple image picker to demonstrate this. The image picker will expose the method `pickImage` to JavaScript, which will return the path of the image when called. ```java -public class ImagePickerModule extends ReactContextBaseJavaModule implements ActivityEventListener { +public class ImagePickerModule extends ReactContextBaseJavaModule { private static final int IMAGE_PICKER_REQUEST = 467081; private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST"; @@ -353,12 +353,36 @@ public class ImagePickerModule extends ReactContextBaseJavaModule implements Act private static final String E_NO_IMAGE_DATA_FOUND = "E_NO_IMAGE_DATA_FOUND"; private Promise mPickerPromise; + + private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() { + + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + if (requestCode == IMAGE_PICKER_REQUEST) { + if (mPickerPromise != null) { + if (resultCode == Activity.RESULT_CANCELED) { + mPickerPromise.reject(E_PICKER_CANCELLED, "Image picker was cancelled"); + } else if (resultCode == Activity.RESULT_OK) { + Uri uri = intent.getData(); + + if (uri == null) { + mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, "No image data found"); + } else { + mPickerPromise.resolve(uri.toString()); + } + } + + mPickerPromise = null; + } + } + } + }; public ImagePickerModule(ReactApplicationContext reactContext) { super(reactContext); // Add the listener for `onActivityResult` - reactContext.addActivityEventListener(this); + reactContext.addActivityEventListener(mActivityEventListener); } @Override @@ -391,28 +415,6 @@ public class ImagePickerModule extends ReactContextBaseJavaModule implements Act mPickerPromise = null; } } - - // You can get the result here - @Override - public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) { - if (requestCode == IMAGE_PICKER_REQUEST) { - if (mPickerPromise != null) { - if (resultCode == Activity.RESULT_CANCELED) { - mPickerPromise.reject(E_PICKER_CANCELLED, "Image picker was cancelled"); - } else if (resultCode == Activity.RESULT_OK) { - Uri uri = intent.getData(); - - if (uri == null) { - mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, "No image data found"); - } else { - mPickerPromise.resolve(uri.toString()); - } - } - - mPickerPromise = null; - } - } - } } ```