Enums And Android Annotation IntDef
Solution 1:
The main idea of IntDef annotation is to use set of int constants like an enum, but without enum. In this case you have to declare all constants manually.
@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
@interface Status {
int IDLE = 0;
int PROCESSING = 1;
int DONE = 2;
int CANCELLED = 3;
}
You can see detailed example here.
Solution 2:
Well, you can't quite do it that way. AppEnums.SERVICE_ERROR will never return int; it will return AppEnums.SERVICE_ERROR. That's the point of enumerated types.
What I can suggest is this:
public static class AppEnums {
public static final int CONNECTION_ERROR = 0;
public static final int SERVICE_ERROR = 1;
}
@IntDef({AppEnums.CONNECTION_ERROR,AppEnums.SERVICE_ERROR})
public @interface ServiceErrors {
}
Copied from Yazazzello's comment below:
IntDef - new Enums for Android development. Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android. so
IntDefwhere designed to replace Enums, you cannot useEnuminIntDefdeclarations
Solution 3:
@Retention(RetentionPolicy.SOURCE)
@IntDef({NOT_STARTED, PENDING, COMPLETED})
public @interface DownloadState {
int NOT_STARTED = 1;
int PENDING = 2;
int COMPLETED = 3;
}
Solution 4:
the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.
NOTE: If the IntDef#flag() attribute is set to true, multiple constants can be combined.
@IntDef(flag = false,value = {AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR})
@Retention(RetentionPolicy.SOURCE)
public @interface AppEnums {
int CONNECTION_ERROR = 0;
int SERVICE_ERROR = 1;
}
NOTE: If the IntDef#flag() attribute is set to true, multiple constants can be combined.
also you can Using @LongDef for long values and @StringDef for StringValues
Post a Comment for "Enums And Android Annotation IntDef"