Enum (enum) for Android development--@IntDef use

Source: Internet
Author: User

Official documentation indicates that Android development should avoid using enums (enum classes) because it takes more than twice times more memory than a static const enum. Ginseng http://developer.android.com/training/articles/memory.html#Overhead

So what should I do if I need to use an enum?

https://noobcoderblog.wordpress.com/2015/04/12/java-enum-and-android-intdefstringdef-annotation/


public class Mainactivity extends Activity {//First define constants public static final int SUNDAY = 0;    public static final int MONDAY = 1;    public static final int Tuesday = 2;    public static final int Wednesday = 3;    public static final int Thursday = 4;    public static final int FRIDAY = 5;     public static final int SATURDAY = 6; Use@IntDef "Wrap" constant;//@Retention define policy//Declare constructor @IntDef ({SUNDAY, monday,tuesday,wednesday,thursday,friday,saturday})     @Retention (retentionpolicy.source) public @interface weekdays {} @WeekDays int currentday = SUNDAY;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         Setcurrentday (Wednesday);         Declaring a variable @WeekDays int today = Getcurrentday ();            Switch (today) {case sunday:break;            Case Monday:break;            Case Tuesday:break;            Case Wednesday:break;            Case Thursday:break;            Case Friday:break;            Case Saturday:break;        Default:break;    }} public void Setcurrentday (@WeekDays int currentday) {this.currentday = Currentday;   }  @WeekDays public int Getcurrentday () {return currentday; }}



In Build.gradle

Compile ' com.android.support:support-annotations:22.0.0 '





 

Enum (enum) for Android development--@IntDef use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.