Skip to content

Commit

Permalink
Add Kotlin examples in Integration with an Android Fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvacca committed Feb 25, 2022
1 parent 67d9c46 commit e447688
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
7 changes: 7 additions & 0 deletions website/core/TabsConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const packageManagers = [
];
const defaultPackageManager = 'npm';

const androidLanguages = [
{label: 'Java', value: 'java'},
{label: 'Kotlin', value: 'kotlin'},
];

const defaultAndroidLanguage = 'kotlin';

const guides = [
{label: 'Expo CLI Quickstart', value: 'quickstart'},
{label: 'React Native CLI Quickstart', value: 'native'},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ id: integration-with-android-fragment
title: Integration with an Android Fragment
---

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

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.

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

Ensure your main Application Java class implements ReactApplication:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
class MyReactApplication: Application(), ReactApplication {...}
```

</TabItem>
<TabItem value="java">

```java
public class MyReactApplication extends Application implements ReactApplication {...}
```

</TabItem>
</Tabs>

Override the required methods `getUseDeveloperSupport`, `getPackages` and `getReactNativeHost`:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
class MyReactApplication: Application(), ReactApplication {

override fun onCreate(): Unit {
super.onCreate()
SoLoader.init(this, false)
}

private val reactNativeHost = object: ReactNativeHost(this) {

override fun getUseDeveloperSupport(): Boolean {
return BuildConfig.DEBUG
}

override fun getPackages(): List<ReactPackage> {
val packages = PackageList(this).getPackages()
// Packages that cannot be autolinked yet can be added manually here
return packages
}
}

override fun getReactNativeHost(): ReactNativeHost {
return reactNativeHost
}
}
```

</TabItem>
<TabItem value="java">

```java
public class MyReactApplication extends Application implements ReactApplication {
@Override
Expand Down Expand Up @@ -51,8 +99,29 @@ public class MyReactApplication extends Application implements ReactApplication
}
```

</TabItem>
</Tabs>

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:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
import android.app.Application

import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.soloader.SoLoader

import java.util.List
```

</TabItem>
<TabItem value="java">

```java
import android.app.Application;

Expand All @@ -65,6 +134,9 @@ import com.facebook.soloader.SoLoader;
import java.util.List;
```

</TabItem>
</Tabs>

Perform a "Sync Project files with Gradle" operation.

### Step 3. Add a FrameLayout for the React Native Fragment
Expand Down Expand Up @@ -101,12 +173,51 @@ Now in your Activity class e.g. `MainActivity.java` you need to add an OnClickLi

Add the button field to the top of your Activity:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
private lateinit var button: Button
```

</TabItem>
<TabItem value="java">

```java
private Button mButton;
```

</TabItem>
</Tabs>

Update your Activity's onCreate method as follows:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
override fun onCreate(savedInstanceState: Bundle): Unit {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)

button = findViewById(R.id.button) as Button
button.setOnClickListener {
val reactNativeFragment = ReactFragment.Builder()
.setComponentName("HelloWorld")
.setLaunchOptions(getLaunchOptions("test message"))
.build()

getSupportFragmentManager()
.beginTransaction()
.add(R.id.reactNativeFragment, reactNativeFragment)
.commit()
}
}
```

</TabItem>
<TabItem value="java">

```java
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -131,12 +242,30 @@ protected void onCreate(Bundle savedInstanceState) {
}
```

</TabItem>
</Tabs>

In the code above `Fragment reactNativeFragment = new ReactFragment.Builder()` creates the ReactFragment and `getSupportFragmentManager().beginTransaction().add()` adds the Fragment to the Frame Layout.

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).

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.

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
private fun getLaunchOptions(String message): Bundle {
val initialProperties = Bundle()
initialProperties.putString("message", message)
return initialProperties
}

```

</TabItem>
<TabItem value="java">

```java
private Bundle getLaunchOptions(String message) {
Bundle initialProperties = new Bundle();
Expand All @@ -145,8 +274,28 @@ private Bundle getLaunchOptions(String message) {
}
```

</TabItem>
</Tabs>

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:

<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
<TabItem value="kotlin">

```kotlin
import android.app.Application

import com.facebook.react.ReactApplication
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.shell.MainReactPackage
import com.facebook.soloader.SoLoader

```

</TabItem>
<TabItem value="java">

```java
import android.app.Application;

Expand All @@ -157,6 +306,9 @@ import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
```

</TabItem>
</Tabs>

Perform a "Sync Project files with Gradle" operation.

### Step 5. Test your integration
Expand Down

0 comments on commit e447688

Please sign in to comment.