MultiWindow launcher android application for android 7.0 nougat

Viewed 325

I am developing a android application as a launcher that will open another application in different window like Taskbar( https://play.google.com/store/apps/details?id=com.farmerbb.taskbar&hl=en) application.

I tried to open another application using component name and reflection method. Issue is when new application opens in another window , my launcher application get killed. How can i stop to kill my launcher application.

This is the code snippet for launching a new application in new window :

 public void launchApplication(String componentName) {
    ActivityOptions options = getActivityOptions(AppConstants.FREEFORM_WORKSPACE_STACK_ID);
    Intent intent = new Intent();
    intent.setComponent(ComponentName.unflattenFromString(componentName));
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    try {
        startActivity(intent, options.toBundle());
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

public  ActivityOptions getActivityOptions(int applicationType) {
    ActivityOptions options = ActivityOptions.makeBasic();
    Integer stackId = null;

    switch (applicationType) {
        case FULLSCREEN_WORKSPACE_STACK_ID:
            stackId = FULLSCREEN_WORKSPACE_STACK_ID;
            break;
        case FREEFORM_WORKSPACE_STACK_ID:
            stackId = FREEFORM_WORKSPACE_STACK_ID;
            break;
        case DOCKED_STACK_ID:
            stackId = DOCKED_STACK_ID;
            break;
    }

    if (stackId != null) {
        try {
            Method method = ActivityOptions.class.getMethod("setLaunchStackId", int.class);
            method.invoke(options, stackId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return options;
}
0 Answers
Related