As you know, android provided Multi-Window support mode in android N. Our application has multi-window support.
But how to test it? How to force test run the app in that mode? I haven't founded any such method in Instrumentation class or anywhere else in documentation. Maybe it is somehow possible with Espresso?
Unfortunately the way provided by azizbekian requires an app which has been loaded in multi-window mode previously, so I want to provide own solution. At the answer I have found how to enter multi-window mode programmatically. Using it I built the complete solution:
UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
//enter multi-window mode
uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN);
//wait for completion, unfortunately waitForIdle doesn't applicable here
Thread.sleep(1000);
//simulate selection of our activity
MotionEvent motionDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,
150, 200, 0);
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
uiAutomation.injectInputEvent(motionDown, true);
motionDown.recycle();
MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
150, 200, 0);
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
uiAutomation.injectInputEvent(motionUp, true);
motionUp.recycle();
//perform test actions below
As you can see, there are two workarounds:
We can't use uiAutomation.waitForIdle to wait entering multi-mode completion
I haven't found a way to select an application in task manager to request focus to our activity. So I just perform some touch event on the possible location of our activity.
After implementing it you'll be able to test the activity as usual with Espresso etc.
From Launch New Activities in Multi-Window Mode:
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.
From docs of Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT:
This flag is only used in split-screen multi-window mode. The new activity will be displayed adjacent to the one launching it. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK. Also, setting FLAG_ACTIVITY_MULTIPLE_TASK is required if you want a new instance of an existing activity to be created.
As shown here how to start activity under test:
#Test
public void customIntentToStartActivity() {
Intent intent = new Intent(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
| Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity = mActivityRule.launchActivity(intent);
}
Note, this is my guess based on documentation, haven't tried it. Although, it seems to me you have to start a "fake" Activity first, and from there launch tested activity in multi-window mode, because "The new activity will be displayed adjacent to the one launching it", so there should be another activity who launches it with specified Intent flags.
Related
Please Please read the question complete before marking it as duplicate or vague
On clicking of a button I want to redirect the user to accessibility settings of the android mobile. Where user can click on the accessibility settings of the application. Here is the code that I am using for the same:
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(dummyIntent, 1);
Problem: When user clicks on, I want that it should redirect back to my application and should not remain on the accessibility screen itself.
It's quite late but i had to make the same stuff. So my suggestion is to use onServiceConnected() overridden method in class that extends AccessibilityService . When user apply accessibility in the settings you can launch intent to desired activity inside onServiceConnected(). But you should keep in mind that after device reboot onServiceConnected() also called, so use some flag to make sure that is not user action.
try this :
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
ActivityContext.startActivityForResult(dummyIntent, 1);
There are so many questions on SO related to this, but no such direct questions I could find.
My app has a main activity M, and a sub activity S.
M launchMode is singleTask because I only ever want one instance running and I want S to exist in the same task as M.
S launchMode is standard, because it can have many instances.
In S, there is an Up button which must always take the user to M.
There are two types of task stack (with S on top):
MSSSS...S
If the user presses Up in S, then the stack should become:
M (where M should be in the same state the user left it)
SSSS...S
If the user presses Up in S, then the stack should become:
M (a new instance)
--
So how to construct the Intent for the Up button...
In the first case, FLAG_ACTIVITY_CLEAR_TOP should do the trick, but if we apply this to the second case then the user is dumped back in Home.
In the second case, FLAG_ACTIVITY_CLEAR_TASK will do the trick, but if we apply this to the first case, we discover that M is finished and restarted.
One might also be tempted to try FLAG_ACTIVITY_NEW_TASK, but then we find hitting Back from M will take the user to the screen where he/she hit Up.
Side note: how is this possible when M is singleTask and the activities all have the same (default) task affinity?
So how to correctly construct the Up Intent in this case?
You cannot construct an UP Intent that will do what you want. Those things are mutually exclusive. What you could do is the following:
Change launch mode of M to singleTop
In M.onCreate(), check if M is at the root of the task by using isTaskRoot(). If it isn't the task root, have M restart itself and clear the task as follows:
Intent relaunch = new Intent(this, M.class);
relauch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(relaunch);
In your UP Intent, add the following flags: Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
This will ensure that if an instance of M already exists in the task, it will clear everything on top of that instance and call onNewIntent() on the existing instance.
If there isn't an existing instance of M in the task, it will launch a new instance on top of the stack. This instance will recognize that it isn't the root activity and it will clear the task and relaunch itself.
Note: This will only work on Android 3.0 and above (API 11 or higher).
Here is a workaround/hack using a (Lollipop) deprecated API:
ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
if (M_CLASS_NAME.equals(am.getRunningTasks(1).get(0).baseActivity.getClassName()) {
// we should be able to just clear top - because M is already running
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else {
// okay to assume M is not running (because it is always root of task)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
I am trying to help my users to turn on their GPS like this:
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
The problem is that it opens the android settings as it was a view from my app, but I want to open it as a new application (android default settings aplication), in other words, outside my app.
Does anyone know how to accomplish what I want?
Thanks!
The code you are executing shows a new activity "on its own app process", if the activity you are calling is not in your application project it mean's that is not in your application context, so my thought is that just because you go back and fall down in the last activity shown you may think is in your app, but that's not the case, the activity is running on it's own process and because of the back stack you are going back to your previous activity.
Hope this Helps.
Regards!
The problem is that it opens the android settings as it was a view from my app,
No it doesn't. It's starting an Activity which is part of the Android Settings app.
but I want to open it as a new application (android default settings aplication), in other words, outside my app.
That's exactly what is happening - it's just opening the Activity which deals with the settings for Location Services rather than opening the default Settings root page.
Don't confuse the generic terms 'app' and 'application' with Android classes such as Application and Activity.
Android is meant to be modular - even though it looks like the Activity which is started with your code is part of your own 'app', in reality what happens is an instance of the native Settings app is created and the Location Services Activity is displayed. This happens entirely outside of your app.
As mentioned in this answer and comment, you need to add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
// add the following line
gpsOptionsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(gpsOptionsIntent);
I am looking for a way to launch another app from within my app but so that the focus is not changed from my app to the app launched.
I.e currently I have the new app launched via a intent, however when this is carried out the new app is launched and becomes the app in view, I need it to be kept in the background with my app still in view.
The reason for this?
I am developing an application for internal use that will act like a lock-screen to the device so although things must happen in the background the 'lock-screen' must always be on top.
I have done some research into intents and launching other apps but can not find anything about what I need.
Hope you can help thank you!
Currently the terminal is called like this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("jackpal.androidterm", "jackpal.androidterm.RemoteInterface"));
intent.setAction("jackpal.androidterm.RUN_SCRIPT");
intent.putExtra("jackpal.androidterm.iInitialCommand", cmdString);
The reason it needs to be running in the background is so that the app can run commands in terminal without the user having access, but then they 'unlock' the screen they need to then be able to view the terminal and what commands are being run etc
You can not startActivity in Background.Instead start the activity and minimise the activity(in your case this activity is of different application) using moveTaskToBack(true);
In your case, put a condition based on your intent and its params and use moveTaskToBack(true); so that activity will be minimised only when your application launches.
This won't be possible, you will have to start a background Service that does the actual work and only launch the Activity you want to navigate to once your foreground Activity is finished. Depending on your architecture, you can store the Activity to call when your foreground Activity is finished and change it from the service. That way you will have your desire behaviour without having to actually call the Activity.
In addition to the answer from #Meher, in the intent for your current starting activity, you can add the flag FLAG_FROM_BACKGROUND.
With this you get rid of the "blinking" effect (the one where your activity shows for one fraction of second while it discovers wether to go to background)
I am trying to show the user information on incoming-call screen, whenever there is an incoming-call. So I have a broadcast receiver listening to incoming calls, which starts the intent service, which subsequently starts an activity (with Theme Dialog).
Now, whenever there is an incoming-call, my activity dialog pops up and shows as intended.
Problem: When the activity dialog is already on the screen and incoming-call happens, there is no new activity dialog with new information. I guess that whenever there is an instance, Android does not creates the new one. So it seems like my problem is "creating multiple instances of an activity".
Please note that I am starting an activity from an intent service using FLAG_NEW_TASK.
Google Doc says :
FLAG_ACTIVITY_NEW_TASK
"When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in."
So, if you want to start a new fresh activity then simply not use this flag only, you should use it with FLAG_ACTIVITY_CLEAR_TASK for the desired result.
For Example:
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
If the above solution is not what you needed, then have a look at
android:launchMode attribute, declare this attribute with the desired options (i.e. as per your need) in activity tag of manifest file.
Hope this will solve the problem.
Use flag FLAG_ACTIVITY_MULTIPLE_TASK which according to the documentation :
Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.
Using this flag along with FLAG_ACTIVITY_NEW_TASK will cause each activity instance to be created as a separate task and thus you can have different dialog pop ups.
Simply add following flags to your Intent .
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);