Android:What is difference between setFlags and addFlags for intent
When you use setFlags
you are replacing the old flags... when you use addFlags
you are appending new flags. Remember, a flag is just a integer which is power of two... in binary, flags look like this: 1, 10, 100, 1000, etc... (which in this case are 1, 2, 4, 8). So, what addFlags
does is appending the integer you pass using the |
operator.
// example...
// value of flags: 1
intent.setFlags(2|4);
// now flags have this value: 110
intent.addFlags(8);
// now flags have this value: 1110
When do I use addFlags or setFlags for the purpose of removing activities in stack?
I am assuming that the app starts with OnboardActivity
and that starts LoginActivity
and then you want to clear them both and launch HomeActivity
?
If that is the case, I would do it this way:
OnboardActivity
launchesLoginActivity
usingstartActivityForResult()
.LoginActivity
returns a result that indicates if the login was successful or not and callsfinish()
.LoginActivity
is no longer in the task.OnboardActivity
checks the result inOnActivityResult()
and, if the login was successful, launchesHomeActivity
(no flags needed) and callsfinish()
on itself.- At this point, both
LoginActivity
andOnboardActivity
are gone, andHomeActivity
is the onlyActivity
in the task.
Difference between Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_TASK_ON_HOME
There is a difference between the 2 snippets. Here's some important background information:
A task contains a stack of activities. A task can be in the foreground or in the background.
Tasks are also "stacked". If you are in task
A
and you start a new taskB
, taskB
is stacked on top of taskA
. If the user presses the BACK key enough times in taskB
, he will eventually end up back in task `A. This is standard Android behaviour.
Your snippet...
Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
...will do 1 of the following things, depending...
- If
Activity
has the same task affinity as the current task (ie: the task from which this code is executing), it will clear the current task (finish all activities in the task) and launch a new instance ofActivity
into the current task. If the user presses the BACK key, this will finishActivity
and also finish the current task (since there is only 1 activity in the task) and return the user to either the HOME screen or the task that started this task (the task that is underneath this task in the task stack). - If
Activity
has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivity
would belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance ofActivity
is created at the root of the task and this task is put on top of the current task (so that whenActivity
finishes, the user is dropped back into the current task). - If
Activity
has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance ofActivity
is created at the root of the task and this task is put on top of the current task (so that whenActivity
finishes, the user is dropped back into the current task).
This code snippet...
Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
...will do 1 of the following things depending...
- If
Activity
has the same task affinity as the current task (ie: the task from which this code is executing) andActivity
is the root activity of the current task, this will do nothing. It will not start a new task, it will not clear any activities, it will not create a new instance ofActivity
, and it will not change the behaviour of what happens when the current task is finished (ie: if the current task was started by another task, when all activities in the current task are finished, it will drop the user back into the previous task in the task stack). - If
Activity
has the same task affinity as the current task (ie: the task from which this code is executing) andActivity
is not the root activity of the current task, this will simply create a new instance ofActivity
and put it on top of the current activity in the current task. It will not start a new task, it will not clear any activities, and it will not change the behaviour of what happens when the current task is finished (ie: if the current task was started by another task, when all activities in the current task are finished, it will drop the user back into the previous task in the task stack). - If
Activity
has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivity
would belong to) andActivity
is the root activity of that existing task, then that existing task is brought to the foreground and that task is decoupled from the task stack (ie: when all activities in that task are finished, it will return the user to the HOME screen and not to the task that started that task). - If
Activity
has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivity
would belong to) andActivity
is not the root activity of that existing task, then that existing task is brought to the foreground and that task is decoupled from the task stack (ie: when all activities in that task are finished, it will return the user to the HOME screen and not to the task that started that task) and a new instance ofActivity
is created and put on top of any existing activities in that task. - If
Activity
has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance ofActivity
is created at the root of the task and the new task is decoupled from the task stack (so that whenActivity
finishes, the user is returned to the HOME screen and not to the task that started it).
and finally, this snippet...
Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
...will do 1 of the following things, depending...
- If
Activity
has the same task affinity as the current task (ie: the task from which this code is executing), it will clear the current task (finish all activities in the task) and launch a new instance ofActivity
into the current task. If the user presses the BACK key, this will finishActivity
and also finish the current task (since there is only 1 activity in the task) and return the user to the HOME screen. - If
Activity
has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivity
would belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance ofActivity
is created at the root of the task and this task is decoupled from the task stack (so that whenActivity
finishes, the user is returned to the HOME screen). - If
Activity
has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance ofActivity
is created at the root of the task and this task is decoupled from the task stack (so that whenActivity
finishes, the user is returned to the HOME screen).
I realize that this answer is long and complicated, but there are just so many different cases. I probably haven't even covered all the possible cases (for example, if Activity
has a special launch mode)...
What does it mean intent.setFlags(805306368) in android
805306368
is equivalent to 0x30000000
in hex and 0x30000000
is used to open the Intent
with the following flags :
Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP
So, its equivalent to use the above combination or 0x30000000
.
From Android docs FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_NEW_TASK :
FLAG_ACTIVITY_SINGLE_TOP = 0x20000000
FLAG_ACTIVITY_NEW_TASK = 0x10000000
So, the combination results in 0x30000000
Also, as mentioned in docs the new task flag i.e,FLAG_ACTIVITY_NEW_TASK
is used to achieve the following behaviour:
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.
and the single top flag i.e, FLAG_ACTIVITY_SINGLE_TOP
is used to achieve the following behaviour, as mentioned in the docs :
If set, the activity will not be launched if it is already running at the top of the history stack.
So, these flags help to resume your activity and prevents from opening a new activity.
android intent setFlags issue
Flags are based on setting individual bits, so the first thing you want to do is convert that value to binary. You can do this using any standard calculator app that allows you to convert between decimal and binary. In this case:
0001 0000 0000 0000 0000 0000 0000 0000
^
Only one bit is set to true (28th bit from the right). All of the Intent flags are declared as hex values (e.g. FLAG_ACTIVITY_NEW_TASK = 0x10000000
) so then you probably just want to convert this to hex and figure out which one matches up.
public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;
And it turns out that's the one we were looking for in this case. Let's say you have one that's more complicated -- a combination of multiple flags:
intent.setFlags(335544320);
One again, convert to binary:
0001 0100 0000 0000 0000 0000 0000 0000
^ ^
So we have two flags here. Split these into two separate values, with only one flag set for each:
0001 0000 0000 0000 0000 0000 0000 0000
0000 0100 0000 0000 0000 0000 0000 0000
Convert to hex:
0x10000000
0x04000000
Looking in the source for Intent
, this maps to:
Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TOP
So your equivalent replacement would be:
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
what is Flags used for?
Simply think of flags as features that you're applying to the object (in this case to the object Window
), and they are represented as integers. You can apply the flags using the final variables in Window
and WindowManager.LayoutParams
.
setFlags
replaces current flags.addFlags
appends more flags and does not replace the current ones.
Make clear about addFlags and setFlags in android.view.Window class
The binary operators are because the field is a bitfield. They use a single integer to hold a lot of settings, and each settings are assigned to different bits. You then use binary operations to combine them and set the bits correctly. This is a common hardware technique, its very space efficient. Generally you'll turn on a bit (a setting) by using OR on it, and remove it by ANDing its inverse. Both of these operations leave the rest of the settings unchanged.
You would never see setFlags(FOO | FOO), because its redundant. All that happens is FOO would be set. You would see setFlags(FOO | BAR), which would set both FOO and BAR.
When you see setFlags(FOO, FOO)- the second parameter is a mask. It allows you to turn fields on and off at the same time, and only the bits in the mask will change. SO any other setting will be kept as it was. The math is basically ((getFlags &~mask) | (value & mask)). You'll see the same thing in both values if you only want to change certain settings, and you want to turn them all on. setFlags(x,x) is equivalent to addFlags(x)
Use of finish() and intent FLAGS
First, I believe you are overriding the flags you set using setFlags
. To set multiple flags, you can use addFlags
, or set all flags at once:
Setting all flags at once:
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Or:
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
For the same effect. Note that addFlags
returns the Intent object, so you can even chain the calls together:
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Second, did you try applying these flags to the intent you send in your SignInActivity's onCreate
method?
Related Topics
Android Sdk Manager Not Installing Components
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity
Save Sensitive Data in React Native
Installation Failed with Message Invalid File
How to Change the Android Startactivity() Transition Animation
The Item You Requested Is Not Available for Purchase
How to Create Topic in Fcm Notifications
How to Declare Git Repository as Dependency in Android Gradle
Android Sdk: Get Raw Preview Camera Image Without Displaying It
Can You Launch the Native Camera App from an HTML 5 Web App
Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location
Android Studio Doesn't Recognize My Device
Android Webview for Facebook Like Button
Edittext in Listview Loses Focus When Pressed on Android 4.X