将 Jetpack Compose 与 Presentation Class 一起使用

时间:2021-04-28 11:45:58

标签: android android-jetpack-compose

我正在尝试在 Android 设备(POS 终端)上使用辅助显示器。

在 onCreate 活动中,我有:

    val mediaRouter = getSystemService(Context.MEDIA_ROUTER_SERVICE) as MediaRouter
    val route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO)
    if (route != null) {
        val display = route.presentationDisplay
        if (display != null) {
            val presentation = TestPresentation(this, display)
            presentation.show()
        }
    }

正确检查演示文稿显示。然后我有我的演讲课:

class TestPresentation(context: Context, display: Display):Presentation(context, display) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    val linearLayout = LinearLayout(context)
    val params = LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
    linearLayout.layoutParams = params

    val composeView = ComposeView(context).apply {
        layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
        setContent {
            Text(text = "Hello")
        }
    }

    linearLayout.addView(composeView)

    setContentView(linearLayout)

}

在运行时出现异常:

java.lang.IllegalStateException: ViewTreeLifecycleOwner not set for this ComposeView. If you are adding this ComposeView to an AppCompatActivity, make sure you are using AppCompat version 1.3+. If you are adding this ComposeView to a Fragment, make sure you are using Fragment version 1.3+. For other cases, manually set owners on this view by using `ViewTreeLifecycleOwner.set()` and `ViewTreeSavedStateRegistryOwner.set()`.

在这种情况下,我如何使用 ViewTreeLifecycleOwner.set() 和 ViewTreeSavedStateRegistryOwner.set() ?

我一直在寻找解决方案,但没有成功。

举个例子就好了。

0 个答案:

没有答案