Programmatically open app in split screen

11k views Asked by At

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'
}
2

There are 2 answers

1
mzc On

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() {
        PackageManager manager = getPackageManager();
        Intent 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;
        }
    }
2
Yazon2006 On

First of all app should be targeted to SDK 24+ version. If it targeted to lower than 24 SDK then split screen mode would not work. Then carefully read here and here

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.

This flag is only used in split-screen multi-window mode. The new activity will be displayed adjacent to the one launching it. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK. Also, setting FLAG_ACTIVITY_MULTIPLE_TASK is required if you want a new instance of an existing activity to be created.

It means that you can't start split screen mode programmatically. You can just try to launch activity in another part of screen if you are already in split screen mode.