Skip to content

Commit 403feb9

Browse files
rshestfacebook-github-bot
authored andcommitted
Migrate ReactActivity (#50871)
Summary: Pull Request resolved: #50871 # Changelog: [Android][Changed] - ReactActivity has been migrated to Kotlin. Reviewed By: cortinico Differential Revision: D73507044 fbshipit-source-id: 936263100ca93dafd643a53e5cb799fd5ed7e584
1 parent 29cc235 commit 403feb9

File tree

6 files changed

+145
-169
lines changed

6 files changed

+145
-169
lines changed

packages/helloworld/android/app/src/main/java/com/helloworld/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class MainActivity : ReactActivity() {
1818
* Returns the name of the main component registered from JavaScript. This is used to schedule
1919
* rendering of the component.
2020
*/
21-
override fun getMainComponentName(): String = "HelloWorld"
21+
override val mainComponentName: String?
22+
get() = "HelloWorld"
2223

2324
/**
2425
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java

Lines changed: 0 additions & 162 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react
9+
10+
import android.content.Intent
11+
import android.content.res.Configuration
12+
import android.os.Bundle
13+
import android.view.KeyEvent
14+
import androidx.appcompat.app.AppCompatActivity
15+
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
16+
import com.facebook.react.modules.core.PermissionAwareActivity
17+
import com.facebook.react.modules.core.PermissionListener
18+
19+
/** Base Activity for React Native applications. */
20+
public abstract class ReactActivity protected constructor() :
21+
AppCompatActivity(), DefaultHardwareBackBtnHandler, PermissionAwareActivity {
22+
23+
public open val reactActivityDelegate: ReactActivityDelegate = createReactActivityDelegate()
24+
25+
public open val reactDelegate: ReactDelegate?
26+
get() = reactActivityDelegate.reactDelegate
27+
28+
protected open val reactHost: ReactHost?
29+
get() = reactActivityDelegate.reactHost
30+
31+
protected val reactNativeHost: ReactNativeHost
32+
get() = reactActivityDelegate.reactNativeHost
33+
34+
protected val reactInstanceManager: ReactInstanceManager
35+
get() = reactActivityDelegate.reactInstanceManager
36+
37+
/**
38+
* Returns the name of the main component registered from JavaScript. This is used to schedule
39+
* rendering of the component. e.g. "MoviesApp"
40+
*/
41+
protected open val mainComponentName: String?
42+
get() = null
43+
44+
/** Called at construction time, override if you have a custom delegate implementation. */
45+
protected open fun createReactActivityDelegate(): ReactActivityDelegate {
46+
return ReactActivityDelegate(this, mainComponentName)
47+
}
48+
49+
override fun onCreate(savedInstanceState: Bundle?) {
50+
super.onCreate(savedInstanceState)
51+
reactActivityDelegate.onCreate(savedInstanceState)
52+
}
53+
54+
override fun onPause() {
55+
super.onPause()
56+
reactActivityDelegate.onPause()
57+
}
58+
59+
override fun onResume() {
60+
super.onResume()
61+
reactActivityDelegate.onResume()
62+
}
63+
64+
override fun onDestroy() {
65+
super.onDestroy()
66+
reactActivityDelegate.onDestroy()
67+
}
68+
69+
override public fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
70+
super.onActivityResult(requestCode, resultCode, data)
71+
reactActivityDelegate.onActivityResult(requestCode, resultCode, data)
72+
}
73+
74+
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean =
75+
reactActivityDelegate.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
76+
77+
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean =
78+
reactActivityDelegate.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event)
79+
80+
override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean =
81+
reactActivityDelegate.onKeyLongPress(keyCode, event) || super.onKeyLongPress(keyCode, event)
82+
83+
@Suppress("DEPRECATION")
84+
@Deprecated("Deprecated in Java")
85+
override fun onBackPressed() {
86+
if (!reactActivityDelegate.onBackPressed()) {
87+
super.onBackPressed()
88+
}
89+
}
90+
91+
override fun invokeDefaultOnBackPressed() {
92+
@Suppress("DEPRECATION") super.onBackPressed()
93+
}
94+
95+
override public fun onNewIntent(intent: Intent) {
96+
if (!reactActivityDelegate.onNewIntent(intent)) {
97+
super.onNewIntent(intent)
98+
}
99+
}
100+
101+
override public fun onUserLeaveHint() {
102+
super.onUserLeaveHint()
103+
reactActivityDelegate.onUserLeaveHint()
104+
}
105+
106+
override fun requestPermissions(
107+
permissions: Array<String>,
108+
requestCode: Int,
109+
listener: PermissionListener?
110+
) {
111+
reactActivityDelegate.requestPermissions(permissions, requestCode, listener)
112+
}
113+
114+
override fun onRequestPermissionsResult(
115+
requestCode: Int,
116+
permissions: Array<String>,
117+
grantResults: IntArray
118+
) {
119+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
120+
reactActivityDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults)
121+
}
122+
123+
override fun onWindowFocusChanged(hasFocus: Boolean) {
124+
super.onWindowFocusChanged(hasFocus)
125+
reactActivityDelegate.onWindowFocusChanged(hasFocus)
126+
}
127+
128+
override fun onConfigurationChanged(newConfig: Configuration) {
129+
super.onConfigurationChanged(newConfig)
130+
reactActivityDelegate.onConfigurationChanged(newConfig)
131+
}
132+
133+
protected fun loadApp(appKey: String?) {
134+
reactActivityDelegate.loadApp(appKey)
135+
}
136+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public ReactInstanceManager getReactInstanceManager() {
110110
return mReactDelegate.getReactInstanceManager();
111111
}
112112

113-
public String getMainComponentName() {
113+
public @Nullable String getMainComponentName() {
114114
return mMainComponentName;
115115
}
116116

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactActivityDelegate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.facebook.react.ReactActivityDelegate
2121
*/
2222
public open class DefaultReactActivityDelegate(
2323
activity: ReactActivity,
24-
mainComponentName: String,
24+
mainComponentName: String?,
2525
private val fabricEnabled: Boolean = false,
2626
) : ReactActivityDelegate(activity, mainComponentName) {
2727

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.io.FileDescriptor
2222
import java.io.PrintWriter
2323

2424
internal class RNTesterActivity : ReactActivity() {
25-
class RNTesterActivityDelegate(val activity: ReactActivity, mainComponentName: String) :
25+
class RNTesterActivityDelegate(val activity: ReactActivity, mainComponentName: String?) :
2626
DefaultReactActivityDelegate(activity, mainComponentName, fabricEnabled) {
2727
private val PARAM_ROUTE = "route"
2828
private lateinit var initialProps: Bundle
@@ -43,6 +43,9 @@ internal class RNTesterActivity : ReactActivity() {
4343
if (this::initialProps.isInitialized) initialProps else Bundle()
4444
}
4545

46+
override val mainComponentName: String?
47+
get() = "RNTesterApp"
48+
4649
override fun onCreate(savedInstanceState: Bundle?) {
4750
super.onCreate(savedInstanceState)
4851

@@ -52,7 +55,7 @@ internal class RNTesterActivity : ReactActivity() {
5255
this.window?.setBackgroundDrawable(ColorDrawable(Color.BLACK))
5356
// register insets listener to update margins on the ReactRootView to avoid overlap w/ system
5457
// bars
55-
getReactDelegate()?.getReactRootView()?.let { rootView ->
58+
reactDelegate?.getReactRootView()?.let { rootView ->
5659
val insetsType: Int =
5760
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()
5861

@@ -71,8 +74,6 @@ internal class RNTesterActivity : ReactActivity() {
7174

7275
override fun createReactActivityDelegate() = RNTesterActivityDelegate(this, mainComponentName)
7376

74-
override fun getMainComponentName() = "RNTesterApp"
75-
7677
override fun dump(
7778
prefix: String,
7879
fd: FileDescriptor?,

0 commit comments

Comments
 (0)