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
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.
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.
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.
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);
}
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With