Fragment Manager State Handler

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.

The FragmentManagerStateHandler is a class that helps restoring fragments when a screen mode transition occurs.

The problem we are facing when we make screen mode transitions is that the activity will be recreated but the UI is not the same.

For example:

  • No need to restore the single-screen fragment when we transition to dual screen mode.
  • No need to restore the dual-screen fragments when we transition to single screen mode.

The component will automatically detect the screen mode we are in and it will restore just the fragments we need, depending on the screen mode.

Important

The component only works with activities from AndroidX.

How to use

class SampleApp : Application() {
    lateinit var surfaceDuoScreenManager: SurfaceDuoScreenManager

    override fun onCreate() {
        super.onCreate()
        surfaceDuoScreenManager = SurfaceDuoScreenManager.getInstance(this)
        FragmentManagerStateHandler.initialize(this, surfaceDuoScreenManager)
    }
}
class MainActivity : AppCompatActivity() {
    companion object {
        private const val FRAGMENT_DUAL_START = "FragmentDualStart"
        private const val FRAGMENT_DUAL_END = "FragmentDualEnd"
        private const val FRAGMENT_SINGLE_SCREEN = "FragmentSingleScreen"
    }

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

        when ((application as SampleApp).surfaceDuoScreenManager.screenMode) {
            ScreenMode.SINGLE_SCREEN -> {
                if (supportFragmentManager.findFragmentByTag(FRAGMENT_SINGLE_SCREEN) == null) {
                    supportFragmentManager.beginTransaction()
                        .replace(
                            R.id.first_container_id,
                            SingleScreenFragment(),
                            FRAGMENT_SINGLE_SCREEN
                        ).commit()
                }
            }
            ScreenMode.DUAL_SCREEN -> {
                if (supportFragmentManager.findFragmentByTag(FRAGMENT_DUAL_START) == null &&
                    supportFragmentManager.findFragmentByTag(FRAGMENT_DUAL_END) == null) {
                    supportFragmentManager.beginTransaction()
                        .replace(
                            R.id.first_container_id,
                            DualStartFragment(),
                            FRAGMENT_DUAL_START
                        ).commit()
                    supportFragmentManager.beginTransaction()
                        .replace(
                            R.id.second_container_id,
                            DualEndFragment(),
                            FRAGMENT_DUAL_END
                        ).commit()
                }
            }
        }
    }
}