Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ class ReactNativeBrownfield private constructor(val reactHost: ReactHost) {
moduleName: String,
reactDelegate: ReactDelegateWrapper? = null,
launchOptions: Bundle? = null,
): FrameLayout = createView(activity, moduleName, reactDelegate, launchOptions, activity)

/**
* Use the owner of the view when embedding React Native inside a fragment or
* another container whose lifetime is shorter than the activity.
* The four-argument overload retains its original JVM signature.
*/
fun createView(
activity: FragmentActivity?,
moduleName: String,
reactDelegate: ReactDelegateWrapper?,
launchOptions: Bundle?,
lifecycleOwner: LifecycleOwner?,
): FrameLayout {
val reactHost = shared.reactHost
val resolvedDelegate =
Expand All @@ -215,12 +228,17 @@ class ReactNativeBrownfield private constructor(val reactHost: ReactHost) {
}

// Register back press callback
activity?.onBackPressedDispatcher?.addCallback(backPressedCallback)
if (lifecycleOwner != null) {
activity?.onBackPressedDispatcher?.addCallback(lifecycleOwner, backPressedCallback)
}
// invoked on the last RN screen exit
resolvedDelegate.setHardwareBackHandler {
backPressedCallback.isEnabled = false
activity?.onBackPressedDispatcher?.onBackPressed()
backPressedCallback.isEnabled = true
try {
activity?.onBackPressedDispatcher?.onBackPressed()
} finally {
backPressedCallback.isEnabled = true
}
}

/**
Expand All @@ -230,7 +248,7 @@ class ReactNativeBrownfield private constructor(val reactHost: ReactHost) {
* In such a case, we set the lifeCycle observer.
*/
if (reactDelegate == null) {
activity?.lifecycle?.addObserver(getLifeCycleObserver(resolvedDelegate))
lifecycleOwner?.lifecycle?.addObserver(getLifeCycleObserver(resolvedDelegate))
}

resolvedDelegate.loadApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class ReactNativeFragment : ReactFragment(), PermissionAwareActivity {
return ReactNativeBrownfield.shared.createView(
activity,
moduleName,
this.reactDelegate as ReactDelegateWrapper
this.reactDelegate as ReactDelegateWrapper,
launchOptions = null,
lifecycleOwner = viewLifecycleOwner
)
}

Expand Down
Loading