Another really cool thing you can do with multi-window is launch new activities directly in that mode. This means if you have other apps that support split-screen, instead of launching them in your current screen, you can launch them in the other split of the screen, so you can use both apps at the same time.
Another really cool thing you can do here is launch your own app’s activities in split screen, by creating another task for that one specific Activity.
This lets you run two parts of your app simultaneously, each owning one part of your screen. Let’s see how to do that.
To start off, open the AddNoteActivity.kt file and update the getIntent() function like so:
fun getIntent(context: Context, shouldLaunchInMultiWindow: Boolean, note: Note? = null) =
Intent(context, AddNoteActivity::class.java).apply {
putExtra(KEY_NOTE, note)
}
You added a new parameter, which you’ll use to check if you should start this Activity in multi-window.
Now add the following code to add all the flags you need to start the Activity in the split screen:
if (shouldLaunchInMultiWindow) {
flags = Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT or
Intent.FLAG_ACTIVITY_MULTIPLE_TASK or
Intent.FLAG_ACTIVITY_NEW_TASK
}
The important flags here are split into two groups, which is fitting! The first group is the LAUNCH_ADJACENT flag. It allows the Activity to launch in split screen.
The Android system will attempt to its best ability to run it in split screen, if possible.
The other group are the two task flags - MULTIPLE_TASK and NEW_TASK. They are both required to allow your app to run multiple tasks of the same app and to start the new Activity in a new task. This means the new Activity will have its own task and stack, and you can run it alongside other parts of the app.
Pretty cool and very powerful! Now, finally, add the new parameter to the NotesActivity.kt file:
private fun showNoteDetails(note: Note? = null) {
startActivity(AddNoteActivity.getIntent(this, isInMultiWindowMode, note))
}
Again, you’re using the isInMultiWindowMode check to learn if you’re currently using the split screen. Now build & run the app and start up split-screen mode.
Once you have the split screen going, click on the add button, and you new Activity should appear in the split screen mode.
Now you can add a new note, and watch how it automatically gets added to the list of notes you have in your NotesActivity. It’s a powerful feature you can use in any creative way you can think of, that suits your app!