Android @Intdef for flags how to use it
Asked Answered
K

1

14

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.

Kristikristian answered 4/1, 2016 at 2:5 Comment(0)
A
19

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);
Adulation answered 4/1, 2016 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.