Programmatically open app in split screen

Koen Van Looveren picture Koen Van Looveren · Jun 19, 2016 · Viewed 6.9k times · Source

How do I open another app in a split screen in the android N (SDK 24)?

In the documentation I found this:


Launch New Activities in Multi-Window Mode

When you launch a new activity, you can hint to the system that the new activity should be displayed adjacent to the current one, if possible. To do this, use the flag Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT. Passing this flag requests the following behaviour:

If the device is in split-screen mode, the system attempts to create the new activity next to the activity that launched it, so the two activities share the screen. The system is not guaranteed to be able to do this, but it makes the activities adjacent if possible. If the device is not in split-screen mode, this flag has no effect. If a device is in freeform mode and you are launching a new activity, you can specify the new activity's dimensions and screen location by calling ActivityOptions.setLaunchBounds(). This method has no effect if the device is not in multi-window mode.


so when I tried this out, the Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT flag does not exist. I installed

  • Android 6.x (N) SDK 24 revision 1
  • Android N Preview SDK N revision 3
  • Android 6.0 (Marshmallow) SDK 23 revision 3

this is my gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "PACKAGENAME"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 2
        versionName "2.4.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'org.jsoup:jsoup:1.8.3'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
}

Answer

mzc picture mzc · Jul 30, 2021

Hi Starting from android 7.0 you can go to Freeform multiwindow mode. Turn on developer options in your device and use following adb commands :

adb shell settings put global enable_freeform_support  1
adb shell settings put global force_resizable_activities  1

After you can use the following function to launch different activities in split screen programmatically:

private void goToSplitMode() {
        Intent i = new Intent(this, SplitMainActivity.class);
        PackageManager manager = getPackageManager();
        i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");

        i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        Rect mBounds = new Rect(300, 0, getScreenWidth(this), getScreenHeight(this));
         mOptions = getActivityOptions(MainActivity.this);
        mOptions = mOptions.setLaunchBounds(mBounds);

        startActivity(i, mOptions.toBundle());

        i = new Intent(this, SplitMainActivity.class);
        mBounds = new Rect(0, 0, 300, getScreenHeight(this));
        mOptions = getActivityOptions(MainActivity.this);
        mOptions = mOptions.setLaunchBounds(mBounds);

        i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        startActivity(i, mOptions.toBundle());
    }

    public static ActivityOptions getActivityOptions(Context context) {
        ActivityOptions options = ActivityOptions.makeBasic();
        int freeform_stackId = 5;
        try {
            Method method = ActivityOptions.class.getMethod("setLaunchWindowingMode", int.class);
            method.invoke(options, freeform_stackId);
        } catch (Exception e) { /* Gracefully fail */
            }

        return options;
    }

public static int getScreenWidth(@NonNull Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
            Insets insets = windowMetrics.getWindowInsets()
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
            return windowMetrics.getBounds().width() - insets.left - insets.right;
        } else {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            return displayMetrics.widthPixels;
        }
    }

    public static int getScreenHeight(@NonNull Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
            Insets insets = windowMetrics.getWindowInsets()
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
            return windowMetrics.getBounds().height() - insets.top - insets.bottom;
        } else {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            return displayMetrics.heightPixels;
        }
    }