Add Activity to onActivityResult listener interface

Summary:
The Android lifecycle is weird: turns out `onActivityResult` is called before `onResume`. This means `getCurrentActivity()` could return the wrong instance, or `null` if the activity was destroyed. To give developers access to the Activity receiving the result (which is also about to become the current activity), pass it as an argumento the listener.

Fixes github issue #8694.

Reviewed By: donyu

Differential Revision: D3704141

fbshipit-source-id: e7e00ccc28114f97415e5beab8c9b10cb1e530be
This commit is contained in:
Felix Oghina 2016-08-13 08:39:31 -07:00 committed by Facebook Github Bot 6
parent d22003a0de
commit fbd2e13910
6 changed files with 22 additions and 9 deletions

View File

@ -121,7 +121,7 @@ public class ReactActivityDelegate {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager()
.onActivityResult(requestCode, resultCode, data);
.onActivityResult(getPlainActivity(), requestCode, resultCode, data);
}
}

View File

@ -146,7 +146,11 @@ public abstract class ReactInstanceManager {
*/
public abstract void onHostDestroy(Activity activity);
public abstract void onActivityResult(int requestCode, int resultCode, Intent data);
public abstract void onActivityResult(
Activity activity,
int requestCode,
int resultCode,
Intent data);
public abstract void showDevOptionsDialog();
/**

View File

@ -607,9 +607,9 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (mCurrentReactContext != null) {
mCurrentReactContext.onActivityResult(requestCode, resultCode, data);
mCurrentReactContext.onActivityResult(activity, requestCode, resultCode, data);
}
}

View File

@ -2,17 +2,19 @@
package com.facebook.react.bridge;
import android.app.Activity;
import android.content.Intent;
/**
* Listener for receiving activity events.
* Listener for receiving activity events. Consider using {@link BaseActivityEventListener} if
* you're not interested in all the events sent to this interface.
*/
public interface ActivityEventListener {
/**
* Called when host (activity/service) receives an {@link Activity#onActivityResult} call.
*/
void onActivityResult(int requestCode, int resultCode, Intent data);
void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data);
/**
* Called when a new intent is passed to the activity

View File

@ -2,6 +2,7 @@
package com.facebook.react.bridge;
import android.app.Activity;
import android.content.Intent;
/**
@ -9,9 +10,15 @@ import android.content.Intent;
*/
public class BaseActivityEventListener implements ActivityEventListener {
@Override
/**
* @deprecated use {@link #onActivityResult(Activity, int, int, Intent)} instead.
*/
@Deprecated
public void onActivityResult(int requestCode, int resultCode, Intent data) { }
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { }
@Override
public void onNewIntent(Intent intent) { }
}

View File

@ -205,9 +205,9 @@ public class ReactContext extends ContextWrapper {
/**
* Should be called by the hosting Fragment in {@link Fragment#onActivityResult}
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
for (ActivityEventListener listener : mActivityEventListeners) {
listener.onActivityResult(requestCode, resultCode, data);
listener.onActivityResult(activity, requestCode, resultCode, data);
}
}