In Android N
while using split screen I want to launch activity
in current active window when user clicks on notification, but Android N always launches activity
in second window if launch by clicking on notification.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(false)
.setContentTitle("Demo Title")
.setContentText("Demo");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("myIntent", "test");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(156, notification);
When get intent
than launch activity
.
Intent in = new Intent(MainActivity.this, SecondActivity.class);
startActivity(in);
ex - I have two apps in foreground like Chrome browser in first window & Facebook in second window now I am searching something in Chrome browser, at this moment I receive notification of Gmail
. Now when I click on Gmail notification than Gmail app open in second window by replacing Facebook but I want that my app notification replace Chrome(whom with user interact) in first window.
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.
Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.
specify in an Intent
to start an Activity in a task your app is not a part of, which includes multi-window windows of other apps.
There are a few possible scenarios:
Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
flag in the intent, with the Intent.FLAG_ACTIVITY_NEW_TASK
flag to start the Activity in another window. This will only work if the system is already in multi-window mode and is not guaranteed. If the system is in freeform mode (as opposed to split-screen) this requests to start the Activity in another free floating window, then you can also specify it's size with .setLaunchBounds().Intent.FLAG_ACTIVITY_NEW_TASK
flag, and the system starts a new task for the Activity.Intent
go to an Activity that is already started and do whatever you want in it. For example start the Activity in another window with Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
..setLaunchBounds()
, but you cannot hijack the window of another app like you want to do.In short: You can either create new windows, or change existing windows you have, you cannot take windows from other apps.
on controlling which side of the screen your new Activity will occupy in split-screen multi-window, then create a new dummy Activity. Instead of starting your SecondActivity
start the dummy Activity. In it, check which side of the screen it was started on. One way I can think of now is to call .getGlobalVisibleRect() on a View
and see how the values compare to the entire screen size. You can get the screen size this way:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
You can also adapt this solution of detecting the keyboard size to detect the location of your window. It's even more hacky though.
Now if you are on the desired screen side, start your SecondActivity
normally, and finish()
the dummy one. If you are on the wrong one, the also use the Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
flag and your Activity will start on the side you want, while the dummy Activity disappears.
This is a dirty hack. I would recommend just letting the system choose where to start your activity.
then make the browser open your app and it will occupy it's task (and window). You can do that by opening a link with an Intent
, which the browser will handle, and from there redirect to a link that opens your app, see Handling App Links and Enabling Deep Links for more information on how to do that. It should work.
You cannot use Activities for this functionality...
Solution is Use Fragments instead of activities...
just replace the fragments whenever you want
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