We would like to add a flag using Java while starting an Activity. But we cannot find the equivalent of setting launchmode="single task" equivalent flag.
thx in advance
It sounds like you are looking for a combination of FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
From the FLAG_ACTIVITY_CLEAR_TOP documentation:
if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
...
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
I was able to solve this with the following combination;
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Hope this helps!
Related
What is difference between using Intent flag "FLAG_ACTIVITY_NEW_TASK" & "FLAG_ACTIVITY_CLEAR_TOP" and launchMode="singleTask"? and What is the difference in setting intent flag as FLAG_ACTIVITY_SINGLE_TOP and setting launchMode to "singleTop".
I understood your question from your comment you replied to Tim. So you want to know the
behavior differences when setting launchModes and when setting
Intentflags to an activity
Answer to your this question is that you set launchMode for an Activity inside AndroidManifest.xml file but the particular launch behavior can be changed in some ways at runtime through the Intent flags FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_NEW_TASK, and FLAG_ACTIVITY_MULTIPLE_TASK etc.
Now lets come to your two more questions that you mentioned in your main question.
What is difference between using Intent flag "FLAG_ACTIVITY_NEW_TASK"
& "FLAG_ACTIVITY_CLEAR_TOP" and launchMode="singleTask"?
FLAG_ACTIVITY_NEW_TASK
When we set this flag through intent and launch that activity. In that case that activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. It means it will create a separate history stack. For example, in your app you have a settings icon, when you click on that you go to settings activity where you have further activities. Here all the actions recorded will start from your setting activity only.
FLAG_ACTIVITY_CLEAR_TOP
As its name is telling clearly, if you start an Activity with this flag in a existing task(please understand task then everything will be very easy to understand), in that case all the activities in stack above this activity will be closed and this will become the last activity or oldest activity in the Task.
singleTask
When you start an activity for which you set launchMode = "singleTask" and there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front.
Your seconde question
What is the difference in setting intent flag as
FLAG_ACTIVITY_SINGLE_TOP and setting launchMode to "singleTop"?
FLAG_ACTIVITY_SINGLE_TOP and lanuchMode = "singleTop"
Both are have same behavior, flag is set at runtime and launchMode in AndroidManifest.xml in the beginning. The behavior is, activity this with this will be the only activity on the top in a Task. If it is already running at the top of the history stack then the activity will not be launched again.
NOTE: the best way to understand the behavior is to follow any tutorial and check it practically. Play around with code and see the behavior.
Here are some useful links:
Launch Modes : https://developer.android.com/reference/android/R.styleable#AndroidManifestActivity_launchMode
Intent flags : https://developer.android.com/reference/android/content/Intent
I think the Android documentation https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP of FLAG_ACTIVITY_CLEAR_TOP explains the difference quite well. The launch modes are quite nicely explained at https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en.
I'm trying to resume an activity from within a broadcast receiver's onReceive() method as follows:
Intent i = new Intent(context, TimerSet.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
However the activity (TimerSet.class) is recreated instead of resumed. The only recommended solution I found to this problem was to use the FLAG_ACTIVITY_REORDER_TO_FRONT but I'm already using it.
Also, using Intent.FLAG_ACTIVITY_NEW_TASK doesn't fit my use case but I get the following exception when I do not provide it:
android:util.AndroidRuntimeException: Calling startActivity() from outside of an
Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you
want?
I am not sure whether this is exactly your problem or not, but I have a situation where I got a notification and I want to start my app without starting a new instance (if it's already running)
I finally figured out that these will work. The FLAG_ACTIVITY_NEW_TASK will not start a new instant if the activity has already been running. However, it will add it to the existing stack. Therefore, we can do a FLAG_ACTIVITY_CLEAR_TOP, so back will bring user to the home screen but not the previous state.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
remove FLAG_ACTIVITY_NEW_TASK flag.
also add this flag ->FLAG_ACTIVITY_CLEAR_TOP. This would prevent new activity to be created if already present.
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);
I know single instance can be achived by setting android:launchMode="singleInstance" in menifest file. but i want to do it on run time . I think it can be achived by setting FLAG but not sure witch one.. plzz help me.. thanks in advance.
In general you can use a combination of Intent.FLAG_ACTIVITY_SINGLE_TOP, Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and Intent.FLAG_ACTIVITY_NEW_TASK to accomplish what you want. However, which one (or ones) to use depends on the situation you have.
This is what u are looking for
Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
UPDATE: android:launchMode="singleInstance" may also be needed in Manifest
singleInstance specifies that the launched or recycled activity will be created on a new or existing task, and no other activities can be stacked on top of it. The system will ensure any activities started from it are put on other tasks.
1 task and 1 activity.
There is no intent flag that I know of that will prevent further activities launched by your activity without having to specify flags in all of your new activity's created intents.
So the answer is this is not possible.
Previous posted answers imitate how this will look, but the behavior goes against the specification.
you can achieve singleInstance behaviour with this flags Intent.FLAG_ACTIVITY_MULTIPLE_TASK, Intent.FLAG_ACTIVITY_NEW_TASK
I have just noticed that parameter FLAG_ACTIVITY_SINGLE_TASK is no more available in the Android developer documentation:
I am just trying to do this:
removeCard.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TASK);
startActivity(removeCard);
I have already seen such a parameter in code found in Google but the Java compiler just tells me it does not exist.
I am only able to start the main activity with this parameter but I have to configure the manifest file to perform this:
android:launchMode = "singleTask"
And this does not work for activities other than main activities launched upon app start.
Does someone know why this parameter disappear for Intent ?
Regards,
Franz
Ok so after having a look around, I managed to figure out that there is no such things as FLAG_ACTIVITY_SINGLE_TASK or FLAG_ACTIVITY_SINGLE_INSTANCE, nor has there even been. This is because you are looking for values which can only be defined through the application's launch mode which is defined in the manifest. Only FLAG_ACTIVITY_SINGLE_TOP is available as an intent flag. Therefore if you want to make use of any of the singleTask, singleInstance, singleTop or standard launch modes, they must be defined in the manifest:
<activity
android:name="com.company.ActivityName"
android:launchMode="singleTask">
</activity>
See the launchMode section in the documentation: http://developer.android.com/guide/topics/manifest/activity-element.html
android:launchMode = "singleTask" should work for all activities when used correctly and are you sure you're not referring the FLAG_ACTIVITY_SINGLE_TASK which is still present and appears to do what you are wanting.
public static final int FLAG_ACTIVITY_SINGLE_TOP
Since: API Level 1 If set, the activity will not be launched if it is
already running at the top of the history stack.
No it is not missing. From developer docs......Using Intent flags
FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().
This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.
That means FLAG_ACTIVITY_NEW_TASK do the same as Singletask do