Skip to content

Fix compose views crashing inside RN Modal component. #51319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -54,6 +54,13 @@ import com.facebook.react.views.modal.ReactModalHostView.DialogRootViewGroup
import com.facebook.react.views.view.ReactViewGroup
import com.facebook.react.views.view.setStatusBarTranslucency
import com.facebook.react.views.view.setSystemBarsTranslucency
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.setViewTreeViewModelStoreOwner
import androidx.lifecycle.ViewModelStoreOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import androidx.savedstate.SavedStateRegistryOwner
import androidx.appcompat.app.AppCompatActivity

/**
* ReactModalHostView is a view that sits in the view hierarchy representing a Modal view.
Expand Down Expand Up @@ -143,6 +150,7 @@ public class ReactModalHostView(context: ThemedReactContext) :

protected override fun onAttachedToWindow() {
super.onAttachedToWindow()
dialog?.enableComposeViewSupport(getCurrentActivity())
(context as ThemedReactContext).addLifecycleEventListener(this)
}

Expand Down Expand Up @@ -595,3 +603,12 @@ public class ReactModalHostView(context: ThemedReactContext) :
}
}
}

public fun Dialog.enableComposeViewSupport(activity: Activity?) {
val decorView = getWindow()?.getDecorView()
if (decorView != null && activity != null) {
decorView.setViewTreeLifecycleOwner(activity as LifecycleOwner)
decorView.setViewTreeViewModelStoreOwner(activity as ViewModelStoreOwner)
decorView.setViewTreeSavedStateRegistryOwner(activity as SavedStateRegistryOwner)
}
}
Loading