Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-O launch on secondary display

The new ActivityOptions setLaunchDisplayId (int launchDisplayId) function in Android-O seems to always crash my app when I try to launch an activity intent.

Both when I launch activities from my own app and when I try to launch other apps i.e. Chrome Canary.

Does anyone know if this is a general problem with the new API's or am I missing something:

A small snippet of my code is below:

options.setLaunchDisplayId(1); startActivity(intent, options);

NOTE I was testing with 'simulate a second screen' enabled (@1080p if it matters).

UPDATE I have tried the ADB command adb shell start com.chrome.canary --display 1, and I get the message:

start: must be root

like image 226
Smiler Avatar asked Mar 23 '17 09:03

Smiler


People also ask

How do I enable second screen on Android?

Click on the 3 dots in the toolbar to open Extended Controls. Choose Displays on the vertical menu. Click on the button Add secondary display. You could choose a pre-defined resolution or set a custom one.

Can Android support multiple displays?

Multi-display. Android 10 (API level 29) supports activities on secondary displays. If an activity is running on a device with multiple displays, users can move the activity from one display to another. Multi-resume applies to multi-screen scenarios as well; several activities can receive user input at the same time.

How is activity launched?

Activity launch behavior is defined by launch modes in the AndroidManifest. xml files of apps, intent flags, and ActivityOptions provided by the caller. Use ActivityOption#setLaunchDisplayId(int) to target a specific display for activity launch. By default, the activity launches on the same display as the caller.


2 Answers

I have connected to the second screen via the new API's with the code below, but as of yet have no way of interacting with it.

 Bundle bdl;
 MediaRouter mediaRouter = (MediaRouter) mContext.getSystemService(Context.MEDIA_ROUTER_SERVICE);
 MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
 if (route != null) {
     Display presentationDisplay = route.getPresentationDisplay();
     bdl = ActivityOptions.makeClipRevealAnimation(mView, left, top, width, height).setLaunchBounds(rect).setLaunchDisplayId(presentationDisplay.getDisplayId()).toBundle();
     Bundle optsBundle = true ? bdl : null;
     Intent intent = new Intent(mContext, SecondaryActivity.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     mContext.startActivity(intent, optsBundle);
 }
like image 87
Smiler Avatar answered Sep 20 '22 10:09

Smiler


Here's a simplified answer which is working for me, built off of @Smiler. I tested this via a Button click in my MainActivity. Thanks for the help!

    ((Button) findViewById(R.id.launchTopScreen)).setOnClickListener(v -> {
        Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.new.app");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
        ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(1);
        startActivity(intent, options.toBundle());
    });

The Intent.FLAG_ACTIVITY_NEW_TASK is extremely important in order to have the new application launch on a different display. Otherwise it'll launch in the same task stack, on the same display.

That 'new application' needs to also have android:resizeableActivity="true" in the manifest. True is the default, but I specified to just be explicit and protect future framework changes.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:resizeableActivity="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
like image 24
Todd DeLand Avatar answered Sep 19 '22 10:09

Todd DeLand