Skip to content

Commit 9f0dede

Browse files
kmagierafacebook-github-bot
authored andcommitted
Allow again for injecting custom root view via ReactActivityDelegate (#26495)
Summary: This change restores the possibility of injecting custom root views via ReactAcitivtyDelegate. It has been used by react-native-gesture-handler library in order to replace default root view with a one that'd route touch events to gesture-handler internal pipeline. The regression happened in d0792d4 where new `ReactDelegate` was introduced to provide support for rendering react native views in both Android fragments and activities. As a part of that change the logic responsible for creating root view has been moved from `ReactActivityDelegate` to `ReactDelegate` rendering `ReactActivityDelegate.createRootView` unused – that is there is no code path that leads to this method being called. Instead `ReactDelegate.createRootView` method has been added which now plays the same role. The custom root view injection was relying on overwriting that method and hence the method is no longer referenced overwriting it has no effect. Following the logic migration out of `ReactActivityDelegate` into `ReactDelegate` we could potentially now start overwriting methods of `ReactDelegate`. However when working with Android's activities in React Native we can only provide an instance of `ReactActivityDelegate` and in my opinion it does not make too much sense to expose also a way to provide own instance of `ReactDelegate`. The proposed fix was to route `ReactDelegate.createRootView` to call `ReactActivityDelegate.createRootView` and this way regaining control over root view creation to `ReactActivityDelgate`. The change of the behavior has been implemented by subclassing `ReactDelegate` directly from `ReactActivityDelegate` and hooking the aforementioned methods together. Thanks to this approach, the change has no effect on `ReactDelegate` being used directly from fragments or in other scenarios where it is not being instantiated from `ReactActivityDelegate`. This fixes an issue reported in software-mansion/react-native-gesture-handler#745 and discussed on 0.61 release thread: react-native-community/releases#140 (comment) ## Changelog [Internal] [Fixed] - Allow for custom root view to be injected via ReactActivityDelegate Pull Request resolved: #26495 Test Plan: 1. Run RNTester, take layout snapshot, see the react root view being on the top of view hierarchy. 2. Run gesture-handler Example app (or some other app that overwrites ReactActivityDelegate.createRootView method), on layout snapshot see custom root view being used. Differential Revision: D17482966 Pulled By: mdvacca fbshipit-source-id: 866f551b8b077bafe1eb9e34e5dccb1240fa935e
1 parent cc36b89 commit 9f0dede

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ReactActivityDelegate(ReactActivity activity, @Nullable String mainCompon
4747
}
4848

4949
protected ReactRootView createRootView() {
50-
return mReactDelegate.createRootView();
50+
return new ReactRootView(getContext());
5151
}
5252

5353
/**
@@ -73,7 +73,12 @@ protected void onCreate(Bundle savedInstanceState) {
7373
String mainComponentName = getMainComponentName();
7474
mReactDelegate =
7575
new ReactDelegate(
76-
getPlainActivity(), getReactNativeHost(), mainComponentName, getLaunchOptions());
76+
getPlainActivity(), getReactNativeHost(), mainComponentName, getLaunchOptions()) {
77+
@Override
78+
protected ReactRootView createRootView() {
79+
return ReactActivityDelegate.this.createRootView();
80+
}
81+
};
7782
if (mMainComponentName != null) {
7883
loadApp(mainComponentName);
7984
}

0 commit comments

Comments
 (0)