make Activity reference in ReactContext WeakReference

Reviewed By: andreicoman11

Differential Revision: D3040724

fb-gh-sync-id: fef5a84907d22d07ae1df82f5d1071c8fae45420
shipit-source-id: fef5a84907d22d07ae1df82f5d1071c8fae45420
This commit is contained in:
Aaron Chiu 2016-03-14 07:50:55 -07:00 committed by Facebook Github Bot 3
parent 688bb17777
commit b006f79bf5
1 changed files with 12 additions and 7 deletions

View File

@ -11,6 +11,7 @@ package com.facebook.react.bridge;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.concurrent.CopyOnWriteArraySet;
import android.app.Activity;
@ -21,8 +22,8 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
import com.facebook.react.bridge.queue.MessageQueueThread;
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
/**
* Abstract ContextWrapper for Android applicaiton or activity {@link Context} and
@ -41,7 +42,7 @@ public class ReactContext extends ContextWrapper {
private @Nullable MessageQueueThread mNativeModulesMessageQueueThread;
private @Nullable MessageQueueThread mJSMessageQueueThread;
private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private @Nullable Activity mCurrentActivity;
private @Nullable WeakReference<Activity> mCurrentActivity;
public ReactContext(Context base) {
super(base);
@ -142,7 +143,7 @@ public class ReactContext extends ContextWrapper {
*/
public void onHostResume(@Nullable Activity activity) {
UiThreadUtil.assertOnUiThread();
mCurrentActivity = activity;
mCurrentActivity = new WeakReference(activity);
for (LifecycleEventListener listener : mLifecycleEventListeners) {
listener.onHostResume();
}
@ -241,7 +242,7 @@ public class ReactContext extends ContextWrapper {
}
public boolean hasCurrentActivity() {
return mCurrentActivity != null;
return mCurrentActivity != null && mCurrentActivity.get() != null;
}
/**
@ -250,8 +251,9 @@ public class ReactContext extends ContextWrapper {
* was called before the context is in the right state.
*/
public boolean startActivityForResult(Intent intent, int code, Bundle bundle) {
Assertions.assertNotNull(mCurrentActivity);
mCurrentActivity.startActivityForResult(intent, code, bundle);
Activity activity = getCurrentActivity();
Assertions.assertNotNull(activity);
activity.startActivityForResult(intent, code, bundle);
return true;
}
@ -261,6 +263,9 @@ public class ReactContext extends ContextWrapper {
* MEMORY LEAKS.
*/
/* package */ @Nullable Activity getCurrentActivity() {
return mCurrentActivity;
if (mCurrentActivity == null) {
return null;
}
return mCurrentActivity.get();
}
}