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);
Related
I was reading this question because I want to create an exit button for my android app. I found the following answer
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but I don't understand why he set the flag Intent.FLAG_ACTIVITY_NEW_TASK. What exactly this flag is gonna do? Can I omit it? Which is the best approach?
By the way I got confused when I read
the current task will simply be brought to the front of the screen
with the state it was last in
from the official documentation (here). I thought that FLAG_ACTIVITY_NEW_TASK will create a new back-stack and hence lose the state it was last in...
This flag is required when you start an activity from outside of an activity,like service or another activity outside the current app. It is actually used for making another instance of the activity as a new task.Remove the flag and it should work. if it does not, try using flag Single top.If you are planning to exit from the app the flag is not required.you can remove it.
I tried using the below mentioned flags while starting the activity.
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_SINGLE_TOP
(used in separate intents)
But it does not work as intended.
Are there any special cases where these are used ?
I have already gone through many articles , everywhere people suggest of using the launch modes in the manifest .
But this is not I want. I want to know that why is it not working . What am I doing wrong.
This is my code.
intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I need to clear the activity stack. All the activity except the current one in SDK 8. I want after logout from any activity I can reach to the Login Activity and All previous Activity will get Removed from the activity stack.
I have tried this solution
Intent i = new Intent(BaseActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
But it is not working. I can't use finish() because user can logout from any activity.
Your Help is Highly appreciable.
Just use this flag on its own Intent. FLAG_ACTIVITY_CLEAR_TASK
Use This
Normally when we launch new activity, it’s previous activities will be kept in a queue like a stack of activities. So if you want to kill all the previous activities, just follow these methods
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
I have got two Beautiful solution of my problem.
SOLUTION 1:
One is by using Broadcast Receiver. That I can broadcast a message to all my activity and onRecive() I can finish all my activity. As this Link says.
SOLUTION 2 (A Much Easier way):
Second is by managing a flag in ShearedPrefrences or in Application Activity. In starting of app (on splash screen) set the flag = false; OnLogout click event, just set the flag true and in OnResume() of every activity check if flag is true then call finish().
It works like a charm :) And please If you can't answer at least don't abuse the question.
I need to clear the back history of activities.
I have 5 Activities.
In the first 4 activities I have to make the back button go to and fro with data filled in all the screens. But as I proceed on 5th screen I need to reset the back history and start intent with new stack. So that after i press back in my 5th screen app should exit. I tried using launchMode , Nohistory and Flags such as NEW_TASK , CLEAR_TOP, CLEAR_TASK but still unable to achieve this. Please help me. Suggest me how to achieve this.
In your comments you suggested that you've tried using flags like:
Intent i = new Intent(MainActivity.this,SecondActivity.Class);
i.addFlags(android.flags.FLAG_CLEAR_TOP | FLAG_NEW_TASK | FLAG_CLEAR_TASK)
But instead try using them like:
Intent intent = new Intent(MainActivity.this,SecondActivity.Class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_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