Friday, February 21, 2014

The 10 Commandments of Android Development

In developing Android, I've found a bunch of common mistakes that people often make. I've made more than one of these myself. While I can, and probably will talk about how to avoid each of these at some point in time, I wanted to at least point out some of the common pitfalls that people often encounter when beginning Android development.

 

 The 10 Commandments of Android Development

 

1. Thou Shalt Have no other SDK Version Targeted, except for the latest released!

2. Thou Shalt Not make unto thee any Network Connection on the Main Thread!

3. Thou Shalt Not make unto thee any Toast without .show()ing it to the world!

4. Thou Thou shalt remember thy variables, to recycle them. Neither thy Bitmaps, nor thy TypedArrays, nor thy Parcels, nor even a reference to thy MotionEvents outside of thine functions shall escape without being .recycle()d!

5. Thou Shalt never create an anonymous inner OnSharedPreferenceChangeListener class, so that thy OnSharedPreferenceChangeListener's days may be long, and not be Garbage Collected. 

6. Thou Shalt Not have any local variables inside a onDraw statement!

7. Thou shalt Not programmatically define a View, but instead shall define it via XML code!

8. Thou Shalt Not ignore the convertView of an ArrayAdapter, but shall always use it to reduce thy memory footprint!

9. Thou Shalt not forget to add <use-preferences> when thou addest new features requiring new permissions.

10. Thou Shalt Not use absolute positions, nor use Scaled Pixels incorrectly, but rather use Density Independent Pixels for thy Views, and Scaled Pixels for thy fonts!.



 

2 comments:

  1. I presume number 5 is Because of memory leaks more concretely context leaks which u can avoid using static inner classes and by the use of weakreferences
    See: http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html

    ReplyDelete
    Replies
    1. You are right that #5 is as a result of context leakage, however, if you aren't careful your class will get garbage collected when you don't intend it to. Basically, all OnSharedPreferenceChangeListeners are WeakReferences, so if you don't declare the class properly, it won't stick around after garbage collection, and might really make a mess of things.

      Delete