February 06, 2016

Android Dev Tips: How to replace Enum by int or String efficiently?

There's a good topic on developer.android.com about how to manage app's memory. I want to touch specific paragraph about enums:
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
Lot of developers don't want to follow this rule, saying this is inconvenient, hard to navigate through code, hard to debug, blah-blah-blah... 

Android guys want to help developers with this. They provided android.support.annotation package with lot of powerful annotations for Android Studio and Lint. I want to explain more about IntDef and StringDef. There two classes may help to replace enums efficiently for both: performance and convenience.

Let's look at two typical enum classes:


two enum declarations

Nothing special, short set of logically named constants. If we replace them by static final variables, then we'll automatically loose completeness of these declarations. It will be set of unlinked constants. Here is good tutorial how to use android annotations. It says:

The @IntDef annotation lets you basically create a "typedef", where you create another annotation which represents the valid integer constants that you expect, and then you decorate your API with this typedef annotation.

It's okay, good idea. But what if we'll go even further? We will place our constants directly in the annotation class, like this:

two annotations with constants inside

It looks a bit more complicated then enum declaration, but let's check how it works in code. Here is the simplest class declaration with two fields representing our enums. All we need to do is just mark both: fields and accessors with the annotations.

simple class with two fields of types int and String marked with annotations declared above

And now let's try to assign some values out of declared constants set. You can see error messages directly in editor.

example of error messages

It also works for switch-statement.

example of using switch-statement

Thus we can replace enum classes by simple constants without losing comfort of using strictly declared types. Beside that we improve performance of our apps.

Source code of this example is here.

79 comments:

  1. Just seen your Article, it amazed me and surpised me with god thoughts that eveyone will benefit from it. It is really a very informative post for all those budding entreprenuers planning to take advantage of post for business expansions. You always share such a wonderful articlewhich helps us to gain knowledge .Thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article.
    Thanks
    DedicatedHosting4u.com

    ReplyDelete
  2. The article is very interesting and very understood to be read, may be useful for the people. I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have to bookmarked to check out new stuff on your post. Thanks for sharing the information keep updating, looking forward for more posts..
    Kindly visit us @
    Madurai Travels | Travels in Madurai
    Best Travels in Madurai
    Cabs in Madurai | Madurai Cabs
    Tours and Travels in Madurai

    ReplyDelete
  3. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks
    DedicatedHosting4u.com

    ReplyDelete
  4. Great. It is good to constantly coming up with creative ideas. Provides much needed knowledge. goal oriented blog posts and always tried to find creative ways to meet goals.

    Thanks
    Online affiliates

    ReplyDelete
  5. Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
    what is seo
    types of seo

    ReplyDelete
  6. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application


    ReplyDelete

  7. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete
  8. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
    Continue

    ReplyDelete
  9. Replies
    1. This is Very very nice article. Everyone should read. Thanks for sharing. Don't miss WORLD'S BEST GAMES

      Delete
  10. GBWhatsApp is a far more convenient and modified version of WhatsApp – available for use by everyone on the Internet. Currently, the application only operates on Android smartphones and is not supported by iOS devices.for more Updates Visit Our click here

    ReplyDelete
  11. Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
    This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

    https://globalonlinepills.com/product/soma-350-mg/


    Buy Soma 350 mg
    Soma Pill
    Buy Soma 350 mg online



    Buy Soma 350 mg online
    Soma Pill
    Buy Soma 350 mg

    ReplyDelete
  12. Dr Driving is one of the my favourite game ever and today I am going to share Dr Driving Mod Apk
    https://www.drdrivingmodapk.xyz/

    ReplyDelete
  13. Whatsapp Group Link - 5000+ Updated Whatsapp Groups
    whatsapp group link Click The link And Join Whatsapp New Groups, Dating, Business promositions Groups

    ReplyDelete
  14. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  15. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  16. Really great article, you guys are doing a great work, keep posting good stuff, If your audience is into Tech do share 30 MiUi 11 Tips and Tricks
    Gizdoc Tech Paradise

    ReplyDelete
  17. this blog is very nice and share good information with us i love it tweakbox for android

    ReplyDelete
  18. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete