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
Related
I'm trying to figure out a way to bring an existing activity to the front and clear all other activities. I think I can do this with Intent.setFlags(int flags) but I'm not certain which flags to use.
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_CLEAR_TOP);
I would think this would work, but it doesn't. It clears all activities including the one I'm trying to bring to the front. Is there a way to do this without clearing the activity I'm trying to bring to the front?
You should use these flags
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
It will clear the activity hierarchy and then create your new one.
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!
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 would like a home button on all my actives so they can bring you to the starting point. I got some great help from stack overflow and a link to the document in http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
I'm assuming I will have to set the FLAG_ACTIVITY_CLEAR_TOP flag,
the document ion has the following line
When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity(). The flags you can use to modify the default behavior are:
Ok I tried the following,
new Intent(this,TellaFortuneActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP );
and
startActivity(i,Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
both gave me errors.
How do I do this?
Intent i=new Intent(this,TellaFortuneActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
I have my launcher activity flag set to singleTop.
when I launch activity from notification bar (with FLAG_ACTIVITY_NEW_TASK in receiver), new activity is created and the previous one is not used.
What can be done to have only one activity in stack?
Edit:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);`
in receiver, seem to be working
use Intent.FLAG_ACTIVITY_CLEAR_TOP
Try to learn use of these different flags. use the link below, it will ne helpful.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
I had to use
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
"Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK" makes launched activity behaves differently. Whenever I launch activity form history It always calls onNewIntent