Surface Duo Screen Manager

Important

This article describes functionality and guidance that is in public preview and may be substantially modified before it's generally available. Microsoft makes no warranties, express or implied, with respect to the information provided here.

When you use the SurfaceDuoScreenManager component, you can bind a ScreenModeListener interface to be notified when the screen mode has changed. To receive the notification you will need to also provide a LifecycleOwner. This is needed because SurfaceDuoScreenManager will notify the ScreenModeListener when the LifecycleOwner triggers the ON_START event.

Keep in mind that you can also add multiple listeners for the same LifecycleOwner.

The manager will make any listener inactive when its LifecycleOwner sends an event of type ON_PAUSE and will remove them when an event of type ON_DESTROY is received from the LifecycleOwner.

class SampleApp : Application() {

    lateinit var surfaceDuoScreenManager: SurfaceDuoScreenManager

    override fun onCreate() {
        super.onCreate()
        surfaceDuoScreenManager = SurfaceDuoScreenManager.getInstance(this)
    }
}
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        (application as SampleApp).surfaceDuoScreenManager
            .addScreenModeListener(this, object: ScreenModeListener {

                override fun onSwitchToSingleScreen() {
                    // TODO: Add single-screen behavior.
                }

                override fun onSwitchToDualScreen() {
                    // TODO: Add dual-screen behavior.
                }
        })
    }
}

If you decide to not be notified about screen mode changes using ScreenModeListeners, you can read the screenMode value directly from the manager.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

         when ((application as SampleApp).surfaceDuoScreenManager.screenMode) {
            ScreenMode.SINGLE_SCREEN -> {
                // TODO: Add single-screen behavior.
            }

            ScreenMode.DUAL_SCREEN -> {
                // TODO: Add dual-screen behavior.
            }
        }
    }
}