Skip to content

Commit

Permalink
Work around some Views not using ThemedReactContext
Browse files Browse the repository at this point in the history
Summary:
ThemedReactContext should be what is stored as the context on all RN views, but some legacy components use other things like an Activity.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D37356865

fbshipit-source-id: bc914cd06a8846037506a50f254995a6e10c8a9c
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jun 22, 2022
1 parent 97291bf commit 777a92d
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
Expand All @@ -36,6 +37,8 @@
public abstract class ViewManager<T extends View, C extends ReactShadowNode>
extends BaseJavaModule {

private static String NAME = ViewManager.class.getSimpleName();

/**
* For View recycling: we store a Stack of unused, dead Views. This is null by default, and when
* null signals that View Recycling is disabled. `enableViewRecycling` must be explicitly called
Expand Down Expand Up @@ -199,8 +202,26 @@ public C createShadowNodeInstance() {
* {@link ViewManager} subclass.
*/
public void onDropViewInstance(@NonNull T view) {
// Some legacy components will return an Activity here instead of a ThemedReactContext
Context viewContext = view.getContext();
if (viewContext == null) {
// Who knows! Anything is possible. Checking instanceof on null is an NPE,
// So this is not redundant.
FLog.e(NAME, "onDropViewInstance: view [" + view.getId() + "] has a null context");
return;
}
if (!(viewContext instanceof ThemedReactContext)) {
FLog.e(
NAME,
"onDropViewInstance: view ["
+ view.getId()
+ "] has a context that is not a ThemedReactContext: "
+ viewContext);
return;
}

// View recycling
ThemedReactContext themedReactContext = (ThemedReactContext) view.getContext();
ThemedReactContext themedReactContext = (ThemedReactContext) viewContext;
int surfaceId = themedReactContext.getSurfaceId();
@Nullable Stack<T> recyclableViews = getRecyclableViewStack(surfaceId);

Expand Down

0 comments on commit 777a92d

Please sign in to comment.