mirror of
https://github.com/status-im/react-native.git
synced 2025-01-16 04:24:15 +00:00
remove activities from module constructors
Summary: Refactor modules that take activities (or activities that implement some interface) as constructor args to not do that. Expose `getCurrentActivity()` in `ReactContext` and use that wherever the activity is needed. public Reviewed By: astreet Differential Revision: D2680462 fb-gh-sync-id: f263b3fe5b422b7aab9fdadd051cef4e82797b0a
This commit is contained in:
parent
7ab17e5ef3
commit
c06efc0831
@ -47,6 +47,18 @@ import com.facebook.react.uimanager.ViewManager;
|
||||
* have a static method, and so cannot (in Java < 8), be one.
|
||||
*/
|
||||
public abstract class ReactInstanceManager {
|
||||
|
||||
/**
|
||||
* Listener interface for react instance events.
|
||||
*/
|
||||
public interface ReactInstanceEventListener {
|
||||
/**
|
||||
* Called when the react context is initialized (all modules registered). Always called on the
|
||||
* UI thread.
|
||||
*/
|
||||
void onReactContextInitialized(ReactContext context);
|
||||
}
|
||||
|
||||
public abstract DevSupportManager getDevSupportManager();
|
||||
|
||||
/**
|
||||
@ -118,6 +130,11 @@ public abstract class ReactInstanceManager {
|
||||
public abstract List<ViewManager> createAllViewManagers(
|
||||
ReactApplicationContext catalystApplicationContext);
|
||||
|
||||
/**
|
||||
* Add a listener to be notified of react instance events.
|
||||
*/
|
||||
public abstract void addReactInstanceEventListener(ReactInstanceEventListener listener);
|
||||
|
||||
@VisibleForTesting
|
||||
public abstract @Nullable ReactContext getCurrentReactContext();
|
||||
|
||||
|
@ -12,10 +12,11 @@ package com.facebook.react;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
@ -95,6 +96,8 @@ import com.facebook.systrace.Systrace;
|
||||
private @Nullable DefaultHardwareBackBtnHandler mDefaultBackButtonImpl;
|
||||
private String mSourceUrl;
|
||||
private @Nullable Activity mCurrentActivity;
|
||||
private final Collection<ReactInstanceEventListener> mReactInstanceEventListeners =
|
||||
new ConcurrentLinkedQueue<>();
|
||||
private volatile boolean mHasStartedCreatingInitialContext = false;
|
||||
private final UIImplementationProvider mUIImplementationProvider;
|
||||
|
||||
@ -406,6 +409,7 @@ import com.facebook.systrace.Systrace;
|
||||
mCurrentReactContext = null;
|
||||
mHasStartedCreatingInitialContext = false;
|
||||
}
|
||||
mCurrentActivity = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -482,6 +486,11 @@ import com.facebook.systrace.Systrace;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addReactInstanceEventListener(ReactInstanceEventListener listener) {
|
||||
mReactInstanceEventListeners.add(listener);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@Override
|
||||
public @Nullable ReactContext getCurrentReactContext() {
|
||||
@ -535,6 +544,10 @@ import com.facebook.systrace.Systrace;
|
||||
for (ReactRootView rootView : mAttachedRootViews) {
|
||||
attachMeasuredRootViewToInstance(rootView, catalystInstance);
|
||||
}
|
||||
|
||||
for (ReactInstanceEventListener listener : mReactInstanceEventListeners) {
|
||||
listener.onReactContextInitialized(reactContext);
|
||||
}
|
||||
}
|
||||
|
||||
private void attachMeasuredRootViewToInstance(
|
||||
|
@ -146,7 +146,6 @@ public class ReactContext extends ContextWrapper {
|
||||
*/
|
||||
public void onPause() {
|
||||
UiThreadUtil.assertOnUiThread();
|
||||
mCurrentActivity = null;
|
||||
for (LifecycleEventListener listener : mLifecycleEventListeners) {
|
||||
listener.onHostPause();
|
||||
}
|
||||
@ -163,6 +162,7 @@ public class ReactContext extends ContextWrapper {
|
||||
if (mCatalystInstance != null) {
|
||||
mCatalystInstance.destroy();
|
||||
}
|
||||
mCurrentActivity = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,4 +239,13 @@ public class ReactContext extends ContextWrapper {
|
||||
mCurrentActivity.startActivityForResult(intent, code, bundle);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the activity to which this context is currently attached, or {@code null} if not attached.
|
||||
* DO NOT HOLD LONG-LIVED REFERENCES TO THE OBJECT RETURNED BY THIS METHOD, AS THIS WILL CAUSE
|
||||
* MEMORY LEAKS.
|
||||
*/
|
||||
/* package */ @Nullable Activity getCurrentActivity() {
|
||||
return mCurrentActivity;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,10 @@
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
/**
|
||||
* Base class for Catalyst native modules that require access to the {@link ReactContext}
|
||||
* instance.
|
||||
@ -27,4 +31,17 @@ public abstract class ReactContextBaseJavaModule extends BaseJavaModule {
|
||||
protected final ReactApplicationContext getReactApplicationContext() {
|
||||
return mReactApplicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the activity to which this context is currently attached, or {@code null} if not attached.
|
||||
*
|
||||
* DO NOT HOLD LONG-LIVED REFERENCES TO THE OBJECT RETURNED BY THIS METHOD, AS THIS WILL CAUSE
|
||||
* MEMORY LEAKS.
|
||||
*
|
||||
* For example, never store the value returned by this method in a member variable. Instead, call
|
||||
* this method whenever you actually need the Activity and make sure to check for {@code null}.
|
||||
*/
|
||||
protected @Nullable final Activity getCurrentActivity() {
|
||||
return mReactApplicationContext.getCurrentActivity();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user