The problem

The Android platform has always supported multi-screen device development. Using Android.app. Presentation can quickly create a sub-screen display page (document), but only the traditional View layout can be used, and the direct use of compose- UI is not supported. As a result, we cannot unify the UI framework when developing multi-screen applications.

Fortunately Android provides a Dialog Compose is implemented, at android.com pose. The UI: UI components the androidx.com pose. The UI. The window. The Dialog, And android. App. The Presentation is inherited from a subclass of Dialog, then we can draw a implementation scheme, the magic to change out a androidx.com pose. The UI. The window. The Presentation will be ready to use.

The solution

  • AndroidPresentation.android.kt

First open the mobile developer options -> Analog Auxiliary Display Device ->720P,1080P(dual screen)

When opened, the screen displays two areas. This is the simulated side screen, which currently doesn’t offer gestures. It’s best to test with a physical dual-screen device like Microsoft’s SuFace Duo.

Use the sample

override fun onCreate(savedInstanceState: Bundle?). {
    super.onCreate(savedInstanceState)
    // Get all screen information (primary screen + secondary screen)
    displayManagerCompat.displays.also { ownDisplayList ->
        setContent { // Set the content
            ownDisplayList.forEach { // Walk through the screen
                if (it.displayId == Display.DEFAULT_DISPLAY) { // Default screen (home screen)
                    composeContent(it) // Combine screen UI
                } else {
                    / / use AndroidPresentation. Android. Kt in the Presentation of the definition of function to open screen display
                    Presentation(it, onDismissRequest = {
                        /** Callback when the secondary screen closes, similar to Dialog*/
                    }) {
                        composeContent(it) // Combine screen UI
                    }
                }
            }
        }
    }
}


/** * combine screen UI */
@Composable
fun composeContent(display: Display) {
    DemoTheme {
        // UI content is omitted. . . }}Copy the code

Running results (for reference only) :

Extended use

The android.app.Presentation constructor supports passing in Service and Application as Context. We can’t use the android.app.Presentation above to implement it. But you can refer to androidx.activity.Com ponentActivity ponentPresentation implementation to customize a own androidx.presentation.Com

The following is the modified version for reference only

  • ComposePresentation.android.kt
  • PresentationViewModelLazy.android.kt

ComposePresentation can be used instead of Android.app. Presentation. For specific steps, please refer to Android Presentation