Android:What is difference between setFlags and addFlags for intent

Android

Android Problem Overview


What is difference between setFlags and addFlags for intent. Could any one explain it please. Help Appreciated.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

intent.setFlags(int num);

This set flag controls how to handle the Intent.setflag mainly depends on type of component being executed by the Intent.It returns the same intent object for chaining multiple calls into a single statement.

intent.addFlags(int num);

This helps to add additional flags to a particular intent with the existing values.this also returns the same intent object for chaining multiple calls into a single statement.

Solution 3 - Android

 public Intent addFlags(int flags) {
    mFlags |= flags;
    return this;
}
public Intent setFlags(int flags) {
    mFlags = flags;
    return this;
}

Just found this from the source code,for reference.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser755499View Question on Stackoverflow
Solution 1 - AndroidCristianView Answer on Stackoverflow
Solution 2 - AndroidSreedevView Answer on Stackoverflow
Solution 3 - AndroidHaldir65View Answer on Stackoverflow