Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow again for injecting custom root view via ReactActivityDelegate #26495

Conversation

kmagiera
Copy link
Contributor

@kmagiera kmagiera commented Sep 18, 2019

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

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.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner labels Sep 18, 2019
@brentvatne brentvatne requested a review from mdvacca September 18, 2019 21:59
Copy link
Contributor

@mdvacca mdvacca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing this @kmagiera

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdvacca is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @kmagiera in 9f0dede.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Sep 19, 2019
grabbou pushed a commit that referenced this pull request Sep 25, 2019
…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
@flyskywhy
Copy link
Contributor

This commit will cause Blank screen for Android after just react-native run-androidas described in #25704 , it will be nice if you improve the compatibility between this commit and the Metro server 😛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Software Mansion Partner: Software Mansion Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants