Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android @Intdef for flags how to use it

I am not clear how to use @Intdef when making it a flag like this:

@IntDef(
  flag = true
  value = {NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})

this example is straight from the docs. What does this actually mean ? does it mean all of them are initially set to true ? if i do a "or" on the following:

NAVIGATION_MODE_STANDARD | NAVIGATION_MODE_LIST

what does it mean ...im a little confused whats happening here.

like image 994
j2emanue Avatar asked Jan 04 '16 02:01

j2emanue


People also ask

What is IntDef Android?

@Retention(value = AnnotationRetention.SOURCE) @Target(allowedTargets = [AnnotationTarget.ANNOTATION_CLASS]) public annotation IntDef. Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.

What is int def?

int. is an abbreviation for internal or for , international. Collins COBUILD Advanced Learner's Dictionary.


1 Answers

Using the IntDef#flag() attribute set to true, multiple constants can be combined.

Users can combine the allowed constants with a flag (such as |, &, ^ ).

For example:

public static final int DISPLAY_OP_1 = 1;
public static final int DISPLAY_OP_2 = 1<<1;
public static final int DISPLAY_OP_3 = 1<<2;

@IntDef (
    flag=true,
    value={
            DISPLAY_OP_1,
            DISPLAY_OP_2,
            DISPLAY_OP_3
    }
)

@Retention(RetentionPolicy.SOURCE)
public @interface DisplayOptions{}

public void setIntDefFlag(@DisplayOptions int ops) {
    ...
}

and Use setIntDefFalg() with '|'

setIntDefFlag(DisplayOptions.DISPLAY_OP1|DisplayOptions.DISPLAY_OP2);
like image 74
fuxi chu Avatar answered Sep 18 '22 12:09

fuxi chu