Petter Hesselberg
Petter Hesselberg

Reputation: 5498

Use react native components in Android app without creating a new Activity

I'm looking at using react-native components across mobile platforms (iOS and Android). The only approach I've seen for adding a react-native component to an Android app is to create an activity that extends ReactActivity.

Is there any way of adding/using react-native components at a lower level of granularity, such as a View?

Upvotes: 5

Views: 1127

Answers (1)

Petter Hesselberg
Petter Hesselberg

Reputation: 5498

It is not necessary to extend the ReactActivity class. But: If you don't, your activity must do certain things that ReactActivity would otherwise do for you. In particular, you must forward some life-cycle events to the ReactInstanceManager:

  • From onPause, forward to reactInstanceManager.onHostPause;
  • From onResume, forward to reactInstanceManager.onHostResume;
  • From onDestroy, forward to reactInstanceManager.onHostDestroy, and unmount your react application.

...and so on. See the source code for ReactActivity and (in particular) ReactActivityDelegate (which does most of the heavy lifting) for details.

(Never mind ReactFragmentActivity, which has been deprecated.)

Upvotes: 1

Related Questions