Skip to content

Commit e2d5b22

Browse files
mdvaccaShikaSDcortinicoSimek
authored
Add kotlin sample code in "Integration with an Android Fragment" documentation (#2977)
Co-authored-by: Andrei Shikov <[email protected]> Co-authored-by: Nicola Corti <[email protected]> Co-authored-by: Simek <[email protected]>
1 parent 2be288b commit e2d5b22

File tree

3 files changed

+287
-0
lines changed

3 files changed

+287
-0
lines changed

docs/integration-with-android-fragment.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ id: integration-with-android-fragment
33
title: Integration with an Android Fragment
44
---
55

6+
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
7+
68
The guide for [Integration with Existing Apps](https://reactnative.dev/docs/integration-with-existing-apps) details how to integrate a full-screen React Native app into an existing Android app as an Activity. To use React Native components within Fragments in an existing app requires some additional setup. The benefit of this is that it allows for a native app to integrate React Native components alongside native fragments in an Activity.
79

810
### 1. Add React Native to your app
@@ -17,12 +19,50 @@ You will need to implement the ReactApplication interface in your main Applicati
1719

1820
Ensure your main Application Java class implements ReactApplication:
1921

22+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
23+
<TabItem value="kotlin">
24+
25+
```kotlin
26+
class MyReactApplication: Application(), ReactApplication {...}
27+
```
28+
29+
</TabItem>
30+
<TabItem value="java">
31+
2032
```java
2133
public class MyReactApplication extends Application implements ReactApplication {...}
2234
```
2335

36+
</TabItem>
37+
</Tabs>
38+
2439
Override the required methods `getUseDeveloperSupport`, `getPackages` and `getReactNativeHost`:
2540

41+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
42+
<TabItem value="kotlin">
43+
44+
```kotlin
45+
class MyReactApplication : Application(), ReactApplication {
46+
override fun onCreate() {
47+
super.onCreate()
48+
SoLoader.init(this, false)
49+
}
50+
private val reactNativeHost =
51+
object : ReactNativeHost(this) {
52+
override fun getUseDeveloperSupport() = BuildConfig.DEBUG
53+
override fun getPackages(): List<ReactPackage> {
54+
val packages = PackageList(this).getPackages().toMutableList()
55+
// Packages that cannot be autolinked yet can be added manually here
56+
return packages
57+
}
58+
}
59+
override fun getReactNativeHost(): ReactNativeHost = reactNativeHost
60+
}
61+
```
62+
63+
</TabItem>
64+
<TabItem value="java">
65+
2666
```java
2767
public class MyReactApplication extends Application implements ReactApplication {
2868
@Override
@@ -51,8 +91,27 @@ public class MyReactApplication extends Application implements ReactApplication
5191
}
5292
```
5393

94+
</TabItem>
95+
</Tabs>
96+
5497
If you are using Android Studio, use Alt + Enter to add all missing imports in your class. Alternatively these are the required imports to include manually:
5598

99+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
100+
<TabItem value="kotlin">
101+
102+
```kotlin
103+
import android.app.Application
104+
105+
import com.facebook.react.PackageList
106+
import com.facebook.react.ReactApplication
107+
import com.facebook.react.ReactNativeHost
108+
import com.facebook.react.ReactPackage
109+
import com.facebook.soloader.SoLoader
110+
```
111+
112+
</TabItem>
113+
<TabItem value="java">
114+
56115
```java
57116
import android.app.Application;
58117

@@ -65,6 +124,9 @@ import com.facebook.soloader.SoLoader;
65124
import java.util.List;
66125
```
67126

127+
</TabItem>
128+
</Tabs>
129+
68130
Perform a "Sync Project files with Gradle" operation.
69131

70132
### Step 3. Add a FrameLayout for the React Native Fragment
@@ -101,12 +163,49 @@ Now in your Activity class e.g. `MainActivity.java` you need to add an OnClickLi
101163

102164
Add the button field to the top of your Activity:
103165

166+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
167+
<TabItem value="kotlin">
168+
169+
```kotlin
170+
private lateinit var button: Button
171+
```
172+
173+
</TabItem>
174+
<TabItem value="java">
175+
104176
```java
105177
private Button mButton;
106178
```
107179

180+
</TabItem>
181+
</Tabs>
182+
108183
Update your Activity's onCreate method as follows:
109184

185+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
186+
<TabItem value="kotlin">
187+
188+
```kotlin
189+
override fun onCreate(savedInstanceState: Bundle) {
190+
super.onCreate(savedInstanceState)
191+
setContentView(R.layout.main_activity)
192+
button = findViewById<Button>(R.id.button)
193+
button.setOnClickListener {
194+
val reactNativeFragment = ReactFragment.Builder()
195+
.setComponentName("HelloWorld")
196+
.setLaunchOptions(getLaunchOptions("test message"))
197+
.build()
198+
getSupportFragmentManager()
199+
.beginTransaction()
200+
.add(R.id.reactNativeFragment, reactNativeFragment)
201+
.commit()
202+
}
203+
}
204+
```
205+
206+
</TabItem>
207+
<TabItem value="java">
208+
110209
```java
111210
@Override
112211
protected void onCreate(Bundle savedInstanceState) {
@@ -131,12 +230,28 @@ protected void onCreate(Bundle savedInstanceState) {
131230
}
132231
```
133232

233+
</TabItem>
234+
</Tabs>
235+
134236
In the code above `Fragment reactNativeFragment = new ReactFragment.Builder()` creates the ReactFragment and `getSupportFragmentManager().beginTransaction().add()` adds the Fragment to the Frame Layout.
135237

136238
If you are using a starter kit for React Native, replace the "HelloWorld" string with the one in your `index.js` or `index.android.js` file (it’s the first argument to the AppRegistry.registerComponent() method).
137239

138240
Add the `getLaunchOptions` method which will allow you to pass props through to your component. This is optional and you can remove `setLaunchOptions` if you don't need to pass any props.
139241

242+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
243+
<TabItem value="kotlin">
244+
245+
```kotlin
246+
private fun getLaunchOptions(message: String) = Bundle().apply {
247+
putString("message", message)
248+
}
249+
250+
```
251+
252+
</TabItem>
253+
<TabItem value="java">
254+
140255
```java
141256
private Bundle getLaunchOptions(String message) {
142257
Bundle initialProperties = new Bundle();
@@ -145,8 +260,28 @@ private Bundle getLaunchOptions(String message) {
145260
}
146261
```
147262

263+
</TabItem>
264+
</Tabs>
265+
148266
Add all missing imports in your Activity class. Be careful to use your package’s BuildConfig and not the one from the facebook package! Alternatively these are the required imports to include manually:
149267

268+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
269+
<TabItem value="kotlin">
270+
271+
```kotlin
272+
import android.app.Application
273+
274+
import com.facebook.react.ReactApplication
275+
import com.facebook.react.ReactNativeHost
276+
import com.facebook.react.ReactPackage
277+
import com.facebook.react.shell.MainReactPackage
278+
import com.facebook.soloader.SoLoader
279+
280+
```
281+
282+
</TabItem>
283+
<TabItem value="java">
284+
150285
```java
151286
import android.app.Application;
152287

@@ -157,6 +292,9 @@ import com.facebook.react.shell.MainReactPackage;
157292
import com.facebook.soloader.SoLoader;
158293
```
159294

295+
</TabItem>
296+
</Tabs>
297+
160298
Perform a "Sync Project files with Gradle" operation.
161299

162300
### Step 5. Test your integration

website/core/TabsConstants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ const packageManagers = [
1919
];
2020
const defaultPackageManager = 'npm';
2121

22+
const androidLanguages = [
23+
{label: 'Java', value: 'java'},
24+
{label: 'Kotlin', value: 'kotlin'},
25+
];
26+
27+
const defaultAndroidLanguage = 'java';
28+
2229
const guides = [
2330
{label: 'Expo CLI Quickstart', value: 'quickstart'},
2431
{label: 'React Native CLI Quickstart', value: 'native'},
@@ -52,10 +59,12 @@ export default {
5259
defaultPackageManager,
5360
defaultPlatform,
5461
defaultSyntax,
62+
defaultAndroidLanguage,
5563
getDevNotesTabs,
5664
guides,
5765
oses,
5866
packageManagers,
5967
platforms,
6068
syntax,
69+
androidLanguages,
6170
};

0 commit comments

Comments
 (0)