Realization of Android 7.0 Multi window Split Screen Mode

  • 2021-11-29 08:28:33
  • OfStack

Since Android 7.0, Google has introduced a new function called "Multi-window Mode", which is often called "Split Screen Mode". So, what's the use of this function? What can we do as developers?

Android 7.0 adds support for displaying multiple APP at the same time. On handheld devices, two APP can run side by side in split screen mode.

Well, that's probably it:

Adaptation of split screen mode

How can we make our APP support split screen mode?

If the project's targetSDKVersion is greater than or equal to 24, you can control whether the entire APP or an Activity supports split screens by setting android: resizeableActivity = ["true" "false"] on the Application or Activity node of the AndroidManifest. xml file. The default value of this property is true, that is, if this property is not set, it can be split by default on devices that support split screen.

If the targetSDKVersion of the project is less than 24, it can be split by default when running on a device that supports split screen. At this time, if you need to prohibit split screen, you need to set android: screenOrientation attributes in Application or Activity nodes of AndroidManifest. xml files to control the screen direction of the whole APP or an Activity, so as to control the whole APP or an Activity to prohibit split screen.

Split screen mode monitoring

Can you monitor whether APP enters split screen mode in the code? The answer is yes. Since APP will execute onMultiWindowModeChanged method when the split screen mode changes, we can rewrite this method in Activity to realize split screen monitoring.


@Override
 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
  super.onMultiWindowModeChanged(isInMultiWindowMode);
  //  Judge whether it is currently in split screen mode 
  if (isInMultiWindowMode) {
   //  Split screen mode has been entered 
  } else {
   //  Split screen mode not entered 
  }
 }

Life Cycle in Split Screen Mode

It should be noted that the life cycle of Activity when entering and exiting split screen mode.

When entering split screen mode, the life cycle of Activity:

onPause()->onStop()->onMultiWindowModeChanged()->onDestroy()->onCreate()->onStart()->onResume()->onPause()

When exiting split screen mode, the life cycle of Activity:

onStop()->onDestroy()->onCreate()->onStart()->onResume()->onPause()->onMultiWindowModeChanged()->onResume()

It can be seen that when entering the split screen mode, Activity executes first onMultiWindowModeChanged Method, and then rebuild yourself. When exiting split-screen mode, Activity rebuilds itself before executing onMultiWindowModeChanged Method.

In this way, there will be a problem. When our APP enters split screen mode, it will be in onMultiWindowModeChanged If there is an operation on UI in the method, the automatic reconstruction after it has no effect. In order to prevent this situation, it is necessary to set the  AndroidManifest.xml Set the following properties on the Activity node of


android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"

When this property is set, Activity will not be rebuilt automatically when entering split screen mode.

Judgment of split screen mode

We can get whether we have entered the split-screen mode at present through isInMultiWindowMode () method of Activity. Returning true means that we have entered the split-screen mode at present, and returning false means that we have not entered the split-screen mode at present.


if (isInMultiWindowMode()) {
  //  Split screen mode has been entered 
 } else {
  //  Split screen mode not entered 
 }

Open Activity in split screen mode

If APP opens Activity in split-screen mode with Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT and Intent.FLAG_ACTIVITY_NEW_TASK flags set for Intent, the newly opened Activity will be displayed on the other side of the current APP. For example, the following code:


Intent intent = new Intent(this, NewActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT|Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

As shown in the figure, the original Activity is displayed in the upper half of the screen after being split, and the newly opened Activity is displayed below the original Activity (the lower half of the screen).


Related articles: