Skip to content

Commit 5cef5ec

Browse files
committed
fix(android): handle ReactInstanceManager deprecation in 0.82
1 parent af6e8a2 commit 5cef5ec

File tree

13 files changed

+291
-78
lines changed

13 files changed

+291
-78
lines changed

android/app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ android {
238238
: reactNativeVersion < v(0, 73, 0)
239239
? "src/reactapplication-pre-0.73/java"
240240
: "src/reactapplication-0.73/java",
241+
242+
// TODO: Remove this block when we drop support for 0.75
243+
// https://github.com/react-native-community/template/commit/f738a366b194dd21d4d2bc14c9215b630714dd70
244+
reactNativeVersion >= v(0, 76, 0)
245+
? "src/reacthost-0.76/java"
246+
: "src/reacthost-legacy/java",
241247
]
242248
}
243249

android/app/src/main/java/com/microsoft/reacttestapp/MainActivity.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ class MainActivity : ReactActivity() {
217217
true
218218
}
219219
R.id.show_dev_options -> {
220-
reactInstanceManager.devSupportManager.showDevOptionsDialog()
220+
val devSupportManager = if (BuildConfig.REACTAPP_USE_BRIDGELESS) {
221+
(application as TestApp).reactHost.devSupportManager
222+
} else {
223+
reactInstanceManager.devSupportManager
224+
}
225+
devSupportManager?.showDevOptionsDialog()
221226
true
222227
}
223228
else -> false

android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentBottomSheetDialogFragment.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.facebook.react.ReactRootView
99
import com.facebook.react.interfaces.fabric.ReactSurface
1010
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
1111
import com.microsoft.reacttestapp.BuildConfig
12+
import com.microsoft.reacttestapp.TestApp
1213

1314
class ComponentBottomSheetDialogFragment : BottomSheetDialogFragment() {
1415

@@ -39,8 +40,10 @@ class ComponentBottomSheetDialogFragment : BottomSheetDialogFragment() {
3940
savedInstanceState: Bundle?
4041
): View {
4142
val activity = requireActivity() as ReactActivity
43+
val application = activity.application as TestApp
4244
if (BuildConfig.REACTAPP_USE_BRIDGELESS) {
43-
val surface = activity.reactActivityDelegate.reactHost?.createSurface(
45+
@Suppress("UNNECESSARY_SAFE_CALL")
46+
val surface = application.reactHost?.createSurface(
4447
activity,
4548
requireNotNull(requireArguments().getString(NAME)),
4649
requireArguments().getBundle(INITIAL_PROPERTIES)
@@ -53,7 +56,7 @@ class ComponentBottomSheetDialogFragment : BottomSheetDialogFragment() {
5356
setIsFabric(BuildConfig.REACTAPP_USE_FABRIC)
5457
@Suppress("DEPRECATION")
5558
startReactApplication(
56-
activity.reactActivityDelegate.reactInstanceManager,
59+
application.reactNativeHost.reactInstanceManager,
5760
requireArguments().getString(NAME),
5861
requireArguments().getBundle(INITIAL_PROPERTIES)
5962
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.microsoft.reacttestapp.react
2+
3+
sealed class BundleSource {
4+
enum class Action {
5+
RESTART,
6+
RELOAD
7+
}
8+
9+
abstract fun moveTo(to: BundleSource): Action
10+
11+
object Disk : BundleSource() {
12+
override fun moveTo(to: BundleSource) = Action.RESTART
13+
}
14+
15+
object Server : BundleSource() {
16+
override fun moveTo(to: BundleSource): Action = when (to) {
17+
Disk -> Action.RESTART
18+
Server -> Action.RELOAD
19+
}
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.microsoft.reacttestapp.react
2+
3+
import android.app.Activity
4+
import android.app.Application.ActivityLifecycleCallbacks
5+
import android.os.Bundle
6+
import java.lang.ref.WeakReference
7+
8+
class CurrentActivityTracker : ActivityLifecycleCallbacks {
9+
private var currentActivity: WeakReference<Activity> = WeakReference(null)
10+
11+
fun get(): Activity? = currentActivity.get()
12+
13+
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
14+
// ignore
15+
}
16+
17+
override fun onActivityStarted(activity: Activity) {
18+
// ignore
19+
}
20+
21+
override fun onActivityResumed(activity: Activity) {
22+
currentActivity = WeakReference(activity)
23+
}
24+
25+
override fun onActivityPaused(activity: Activity) {
26+
// ignore
27+
}
28+
29+
override fun onActivityStopped(activity: Activity) {
30+
if (activity == currentActivity.get()) {
31+
currentActivity.clear()
32+
}
33+
}
34+
35+
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
36+
// ignore
37+
}
38+
39+
override fun onActivityDestroyed(activity: Activity) {
40+
// ignore
41+
}
42+
}

android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
1010
import com.facebook.soloader.SoLoader
1111
import com.microsoft.reacttestapp.manifest.Manifest
1212
import com.microsoft.reacttestapp.manifest.ManifestProvider
13+
import com.microsoft.reacttestapp.react.MainReactNativeHost
1314
import com.microsoft.reacttestapp.react.ReactBundleNameProvider
14-
import com.microsoft.reacttestapp.react.TestAppReactNativeHost
1515
import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents
1616

1717
class TestApp :
@@ -28,11 +28,11 @@ class TestApp :
2828
override val reactHost: ReactHost
2929
get() = getDefaultReactHost(this.applicationContext, reactNativeHost)
3030

31-
override val reactNativeHost: TestAppReactNativeHost
31+
override val reactNativeHost: MainReactNativeHost
3232
get() = reactNativeHostInternal
3333

3434
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
35-
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
35+
private lateinit var reactNativeHostInternal: MainReactNativeHost
3636

3737
fun reloadJSFromServer(activity: Activity, bundleURL: String) {
3838
reactNativeHostInternal.reloadJSFromServer(activity, bundleURL)
@@ -45,8 +45,7 @@ class TestApp :
4545
SoLoader.init(this, false)
4646

4747
reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot)
48-
reactNativeHostInternal =
49-
TestAppReactNativeHost(this, reactNativeBundleNameProvider)
48+
reactNativeHostInternal = MainReactNativeHost(this, reactNativeBundleNameProvider)
5049

5150
val eventConsumers = PackageList(this).packages
5251
.filter { it is ReactTestAppLifecycleEvents }

android/app/src/reactapplication-0.76/java/com/microsoft/reacttestapp/TestApp.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import com.facebook.react.soloader.OpenSourceMergedSoMapping
1111
import com.facebook.soloader.SoLoader
1212
import com.microsoft.reacttestapp.manifest.Manifest
1313
import com.microsoft.reacttestapp.manifest.ManifestProvider
14+
import com.microsoft.reacttestapp.react.MainReactNativeHost
1415
import com.microsoft.reacttestapp.react.ReactBundleNameProvider
15-
import com.microsoft.reacttestapp.react.TestAppReactNativeHost
1616
import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents
1717

1818
class TestApp :
@@ -30,11 +30,11 @@ class TestApp :
3030
get() = getDefaultReactHost(this.applicationContext, reactNativeHost)
3131

3232
@Suppress("OVERRIDE_DEPRECATION")
33-
override val reactNativeHost: TestAppReactNativeHost
33+
override val reactNativeHost: MainReactNativeHost
3434
get() = reactNativeHostInternal
3535

3636
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
37-
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
37+
private lateinit var reactNativeHostInternal: MainReactNativeHost
3838

3939
fun reloadJSFromServer(activity: Activity, bundleURL: String) {
4040
reactNativeHostInternal.reloadJSFromServer(activity, bundleURL)
@@ -46,8 +46,7 @@ class TestApp :
4646
SoLoader.init(this, OpenSourceMergedSoMapping)
4747

4848
reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot)
49-
reactNativeHostInternal =
50-
TestAppReactNativeHost(this, reactNativeBundleNameProvider)
49+
reactNativeHostInternal = MainReactNativeHost(this, reactNativeBundleNameProvider)
5150

5251
val eventConsumers = PackageList(this).packages
5352
.filter { it is ReactTestAppLifecycleEvents }

android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import com.facebook.react.ReactApplication
88
import com.facebook.soloader.SoLoader
99
import com.microsoft.reacttestapp.manifest.Manifest
1010
import com.microsoft.reacttestapp.manifest.ManifestProvider
11+
import com.microsoft.reacttestapp.react.MainReactNativeHost
1112
import com.microsoft.reacttestapp.react.ReactBundleNameProvider
12-
import com.microsoft.reacttestapp.react.TestAppReactNativeHost
1313
import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents
1414

1515
class TestApp :
@@ -24,7 +24,7 @@ class TestApp :
2424
}
2525

2626
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
27-
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
27+
private lateinit var reactNativeHostInternal: MainReactNativeHost
2828

2929
fun reloadJSFromServer(activity: Activity, bundleURL: String) {
3030
reactNativeHostInternal.reloadJSFromServer(activity, bundleURL)
@@ -37,8 +37,7 @@ class TestApp :
3737
SoLoader.init(this, false)
3838

3939
reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot)
40-
reactNativeHostInternal =
41-
TestAppReactNativeHost(this, reactNativeBundleNameProvider)
40+
reactNativeHostInternal = MainReactNativeHost(this, reactNativeBundleNameProvider)
4241

4342
val eventConsumers = PackageList(this).packages
4443
.filter { it is ReactTestAppLifecycleEvents }

0 commit comments

Comments
 (0)