0

I am trying to use a secondary display on an android device (POS terminal).

In the activities onCreate, I have :

    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()
        }
    }

which correctly checks for a presentation display. Then I have my presentation class :

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)

}

At runtime I get an exception :

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()`.

How do I use ViewTreeLifecycleOwner.set() and ViewTreeSavedStateRegistryOwner.set() in this case ?

I've been searching for a solution but had no luck.

An example would be great.

john4663
  • 11
  • 2
  • 1
    See [Sam Edwards' implementation of a system overlay using Compose UI](https://gist.github.com/handstandsam/6ecff2f39da72c0b38c07aa80bbb5a2f) for an example. – CommonsWare Apr 28 '21 at 11:50
  • Maybe this answer helps you https://stackoverflow.com/questions/67423907/possible-to-use-layout-a-compose-view-in-and-activity-written-in-java/67424617#67424617 – nglauber May 08 '21 at 02:41

0 Answers0