Now that you’ve set up the resizability support, you can move onto adding more checks and lifecycle handling to your app.
Let’s do that, by adding checks when the user starts the split screen mode, to stop the editing process when adding a new note. Open the AddNoteActivity.kt and add the following code:
private var wasInEditMode: Boolean = false
This flag will represent if the user was in edit mode, before entering the split screen mode. Now add additional checks to the initUi() function, to set up this flag and handle the logic based on the flag:
// initUi()
if (!isChecked) {
...
} else {
wasInEditMode = true
}
When the UI is set up, if the edit option is checked as true, you’ll change the flag to be true. This is to remember if you’ve been in the edit mode before changing to split screen.
Now, to handle the multi-window configuration, there is a special lifecycle callback, called onMultiWindowModeChanged(). Override that function:
override fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean, newConfig: Configuration?) {
super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig)
}
It receives two parameters - if the screen is in multi-window mode or not, and the new screen configuration. The configuration holds data such as the new screen size, orientation, screen mode, screen type, and much more. It’s a very useful object to use to learn about your new configuration and adjust accordingly.
Since you’ve overriden the function, you can add special logic to it. Add the following code, to turn off editing when switching to split-screen, and to turn it back on again when the user leaves split-screen:
if (isInMultiWindowMode) {
binding.editSwitch.isChecked = false
} else if (!isInMultiWindowMode && wasInEditMode) {
binding.editSwitch.isChecked = true
wasInEditMode = false
}
Based on the isInMultiWindowMode and the wasInEditMode flags, you change the editSwitch mode. If the user enters the split-screen, you’ll turn off editing, to make sure everything is saved.
If the user leaves split-screen, you’ll turn on editing again, but only if the user was previously in editing mode. Now build and run the app to test out your new lifecycle handling.
Enable the edit mode before opening the app in split-screen. Now once you’ve done that, change to split-screen mode and see what happens!
The app is now not in the editing mode! Let’s leave the split-screen mode again, and see if we end up in the editing mode.
We do! Awesome! The onMultiWindowModeChanged() function is amazing as it lets you react to split-screen changes. You can completely change your UI if such a change happens, change the logic of features, the number of items on the screen and more.
It’s one of the two places in your code to react to split screen changes. You’ll learn about the next function you can use in the next episode!