Should I strictly avoid using enums on Android?

后端 未结 7 1875
不知归路
不知归路 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:05

    In addition to previous answers, I would add that if you are using Proguard (and you should definitely do it to reduce size and obfuscate your code), then your Enums will be automatically converted to @IntDef wherever it is possible:

    https://www.guardsquare.com/en/proguard/manual/optimizations

    class/unboxing/enum

    Simplifies enum types to integer constants, whenever possible.

    Therefore, if you have some discrete values and some method should allow to take only this values and not others of the same type, then I would use Enum, because Proguard will make this manual work of optimizing code for me.

    And here is a good post about using enums from Jake Wharton, take a look at it.

    As a library developer, I recognize these small optimizations that should be done as we want to have as little impact on the consuming app's size, memory, and performance as possible. But it's important to realize that [...] putting an enum in your public API vs. integer values where appropriate is perfectly fine. Knowing the difference to make informed decisions is what's important

    0 讨论(0)
  • 2020-11-29 16:05

    Should I strictly avoid using enums on Android?

    No. "Strictly" means they are so bad, they should not be used at all. Possibly a performance issues might arise in an extreme situation like many many many (thousands or millions of) operations with enums (consecutive on the ui thread). Far more common are the network I/O operations that should strictly happen in a background thread. The most common usage of enums is probably some kind of type check - whether an object is this or that which is so fast you won't be able to notice a difference between a single comparison of enums and a comparison of integers.

    Can someone please clarify which one is the best way to represent the same in Android?

    There is no general rule of thumb for this. Use whatever works for you and helps you get your app ready. Optimize later - after you notice there's a bottleneck that slows some aspect of your app.

    0 讨论(0)
  • 2020-11-29 16:13

    If the enums simply have values, you should try to use IntDef/StringDef , as shown here:

    https://developer.android.com/studio/write/annotations.html#enum-annotations

    Example: instead of :

    enum NavigationMode {NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS} 
    

    you use:

    @IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
    @Retention(RetentionPolicy.SOURCE)
    public @interface NavigationMode {}
    
    public static final int NAVIGATION_MODE_STANDARD = 0;
    public static final int NAVIGATION_MODE_LIST = 1;
    public static final int NAVIGATION_MODE_TABS = 2;
    

    and in the function that has it as a parameter/returned value , use:

    @NavigationMode
    public abstract int getNavigationMode();
    
    public abstract void setNavigationMode(@NavigationMode int mode);
    

    In case the enum is complex, use an enum. It's not that bad.

    To compare enums vs constant values, you should read here:

    http://hsc.com/Blog/Best-Practices-For-Memory-Optimization-on-Android-1

    Their example is of an enum with 2 values. It takes 1112 bytes in dex file compared to 128 bytes when constant integers are used . Makes sense, as enums are real classes, as opposed to how it works on C/C++ .

    0 讨论(0)
  • 2020-11-29 16:19

    With Android P, google has no restriction/objection in using enums

    The documentation has changed where before it was recommended to be cautious but it doesn't mention it now. https://developer.android.com/reference/java/lang/Enum

    0 讨论(0)
  • 2020-11-29 16:22

    Two facts.

    1, Enum is one of the most powerful feature in JAVA.

    2, Android phone usually has a LOT of memory.

    So my answer is NO. I will use Enum in Android.

    0 讨论(0)
  • 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<String, Values> 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<String, @Values Integer> 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.

    0 讨论(0)
提交回复
热议问题