Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing focus to 2nd app in multi-window

I have my two Android apps running in multi-window mode on a Tablet; App1 and App2. App1 is in focus, I send a broadcast from App1 to App2. There I open a web url externally, using startActivity. But that web URL opens in App1 instead of App2, because App1 was in focus. Though I launched the web url from App2.

I want to open the web url in App2. So I'll probably need to bring App2 to focus from my code first? how to do that?

like image 888
M. Usman Khan Avatar asked Jul 15 '19 09:07

M. Usman Khan


1 Answers

Following the documentation, you can ask Android to open a given Intent in the adjacent focus window using the FLAG_ACTIVITY_LAUNCH_ADJACENT.

Example:

Intent intent = new Intent(getActivity(), Browser.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
getActivity().startActivity(intent);

Documentation extract:

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 intent flag FLAG_ACTIVITY_LAUNCH_ADJACENT. Passing this flag requests the following behavior:

- 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.

Note: If you launch an activity within a task stack, the activity replaces the activity on the screen, inheriting all of its multi-window properties. If you want to launch the new activity as a separate window in multi-window mode, you must launch it in a new task stack.

like image 57
Hugo Gresse Avatar answered Nov 15 '22 00:11

Hugo Gresse