tags:

views:

298

answers:

4

Are there pitfalls or the points to remember while programming for Android? I think the list will include topics on Multithreading, Persistent Storage, etc.

+2  A: 

I wouldn't call them pitfalls per se, but always remember to take into account that this is not a computer that's plugged into a wall that can just be upgraded in various ways. You have an upgrade cycle of about every 2 years (the length of a standard mobile contract these days) and the hardware is (A) not the fastest and (B) static during that time.

Things to take into consideration:

1) How does the things your app does affect battery life? Are you splashing bright graphics all over the place? Running a lot of threads in the background? Services?

2) How much space does your application need to take up on the device? Is the information something that can be kept on a server and transmitted to the device for temporary use only when it's needed?

3) In regards to #2, is your app tolerant of bad/nonexistent network/mobile connections? How does it perform on the EDGE network vs 3G?

I'm sure you can come up with more but this is what I keep in mind while I'm writing my apps.

MattC
+3  A: 

Android Developers has good post about avoiding memory leaks due to keeping Context references. There are a lot of other interesting posts there too.

jstevej
+7  A: 

There are many things that could be said here.

The Android videos from Google I/O 2009 cover most of the aspects that should be kept in mind, when programming on Android. In fact, the http://android-developers.blogspot.com/ articles are the source, on which these presentations expand, and seeing them explained from some of the best Google engineers (and as a bonus you'll get a Q&A section) is a must for every Android developer, IMO.

Some of the things that could be mentioned:

  • Don't use floats, when you can achieve similar results with integers, because Android doesn't have native support for floating point values.

  • Use the debugging tools extensively, to optimize both performance and maintainability, and to avoid common pitfalls like ViewGroup redundancy in UI design, or unnecessary multiple calls to heavier methods (View.inflate(), findViewById(), setImageResource()).

  • Bundle your background service calls, otherwise you are waking up the OS unnecessarily and too often, while risking other services piggy-backing your call (which results in severely reduced battery life)

  • Prefer SAX-parsers over DOM-parsers, you lose time while implementing them, but you win time in your app's performance (and your device's availability)

  • Keep your UI manipulations on your UI thread, because the interface toolkit is not thread-safe

  • Keep in mind that orientation change destroys and creates your Activity again (I learned that the hard and painful way - this is how I started to follow the android-developers' blog)

...and many others.

Dimitar Dimitrov
A: 

If you want to run in the background without being shut down you have to put a notification in the notification bar.

Christian