Should I strictly avoid using enums on Android?

后端 未结 7 1925
不知归路
不知归路 2020-11-29 15:36

I used to define a set of related constants like Bundle keys together in an interface like below:

public interface From{
    String LOGIN_SCREEN         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 16:22

    I like to add, that you can not use @Annotations when you declare a List<> or Map<> where either key or value is of one of your annotation interfaces. You get the error "Annotations are not allowed here".

    enum Values { One, Two, Three }
    Map myMap;    // This works
    
    // ... but ...
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ONE, TWO, THREE})
    public @interface Values {}
    
    Map myMap;    // *** ERROR ***
    

    So when you need to pack it into a list/map, use enum, as they can be added, but @annotated int/string groups can not.

提交回复
热议问题