Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android N: How to launch activity in current active window instead of second window when click on notification in split-screen?

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.

like image 897
Nikhil Avatar asked Sep 08 '16 11:09

Nikhil


People also ask

What is restrict to launch in Android?

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.

Can you start an activity from a service?

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.

How does Android know which activity to run first?

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.


2 Answers

You cannot

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:

  1. You start an Activity from another Activity, it will be started in the previous Activity's task (and window).
  2. You can include the 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().
  3. You start an Activity from a Service, since a Service cannot be in a task, you must specify the Intent.FLAG_ACTIVITY_NEW_TASK flag, and the system starts a new task for the Activity.
  4. You start an Activity from a Notification (PendingIntent), like you do:
    • You can make the 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.
    • If there are no tasks/windows with your Activities, the system will choose where to start it, and you cannot control that. You can control where to start you Activity in freeform mode with .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.

But if you insist

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.

If you always want to replace an opened browser

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.

like image 132
Nohus Avatar answered Sep 18 '22 18:09

Nohus


You cannot use Activities for this functionality...

Solution is Use Fragments instead of activities...

just replace the fragments whenever you want

like image 36
Rajappa R Avatar answered Sep 18 '22 18:09

Rajappa R