diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java index 00e5d0a6501763..cad2c17d7b9f0f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java @@ -28,6 +28,7 @@ import com.facebook.yoga.YogaMeasureMode; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.Stack; /** @@ -41,6 +42,8 @@ public abstract class ViewManager private static final String NAME = ViewManager.class.getSimpleName(); + private @Nullable Optional> mDelegate = Optional.empty(); + /** * 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 @@ -88,7 +91,7 @@ protected void setupViewRecycling() { * @param props {@link ReactStylesDiffMap} props to update the view with */ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) { - final ViewManagerDelegate delegate = getDelegate(); + final ViewManagerDelegate delegate = getOrCreacteViewManagerDelegate(); if (delegate != null) { ViewManagerPropertyUpdater.updateProps(delegate, viewToUpdate, props); } else { @@ -109,11 +112,14 @@ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) * @return an instance of {@link ViewManagerDelegate} if the props of the view managed by this * view manager should be set via this delegate */ - @Nullable - protected ViewManagerDelegate getDelegate() { + protected @Nullable ViewManagerDelegate getDelegate() { return null; } + private @Nullable ViewManagerDelegate getOrCreacteViewManagerDelegate() { + return mDelegate.orElseGet(this::getDelegate); + } + /** Creates a view with knowledge of props and state. */ public @NonNull T createView( int reactTag, @@ -306,7 +312,7 @@ public void receiveCommand(@NonNull T root, int commandId, @Nullable ReadableArr * @param args optional arguments for the command */ public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args) { - final ViewManagerDelegate delegate = getDelegate(); + final ViewManagerDelegate delegate = getOrCreacteViewManagerDelegate(); if (delegate != null) { delegate.receiveCommand(root, commandId, args); }