Aug 30, 2011

Android tips for developers

1. Be resolution independent. Check out “layoutopt” and “hierarchyviewer” that come with Android SDK under “tools”. They help analyzing and optimizing layouts.

2. Respect and use an application lifecycle.

3. In order to avoid friendly “Force Close” popups from your applications, use Android’s “AsyncTask” which will allow to execute a certain activity in background.

4. Only use a WakeLock when you need one with as minimum level as possible: PARTIAL_WAKE_LOCK, SCREEN_DIM_WAKE_LOCK, FULL_WAKE_LOCK. Here is more about PowerManager.

5. Always check whether “network connection” is enabled on a device, before attempting to transfer data.
6. Avoid creating objects unless you really need to, try reusing Android’s API instead. Creating an object in a “desktop” world is relatively cheap, however in such a resource constraint environment as a mobile phone, it will drastically impact the performance.. not in a good way.

7. Externalize resources: localization / optimized layouts / strings / array of strings / etc.. Android compiles them into a list of internal resources by assigning an integer ID to each of them, hence making it “cheaper” at runtime, and easier to change => since they are defined in a single location.

8. Think about what an absolute minimum amount of updates / syncs you can do, and stick to this minimum. This will greatly improve battery life as well resource usage by the application.

9. Think of hiring or delegating UI to people who are designers. Beauty is important.

10. Consider using a “non sticky” services when appropriate.

11.  Respect a “Back” button: make sure it actually brings user back to a previous state rather than to another state of the application’s flow.

12. Do not use foreground services unless you absolutely need to.And if you do use foreground services, use an ongoing notification ( which, starting from Android 2.0, used automatically, if a service is started as a foreground one, but just to keep something in mind to be used for older OS versions )

13. Kill your own services via stopSelf()

14. Don’t use undocumented ( not officially supported ) APIs => Next Android release your app is going to break.

15. Use Intents and Intent Filters to Leverage Other Apps.

16. Prefer Alarms and Intent Receivers to Services.

17. Make use of onPause()/onResume to save or close what does not need to be opened the whole time.

18. Love RelativeLayout: Most of the tutorials use LinearLayout, but you will find that RelativeLayout is truly useful. Many layouts, like the GridLayout aren't used much at all. Play around with RelativeLayout.

19. Use empty layout items: You will often use empty items in your layouts just for positioning other layouts. For example, you might use an empty TextField, of width=0 and height=0 and centerInParent='True' just to anchor things relative to the middle of the screen. Also, you might have an empty TextField or LinearLayout so that you can give a layout_weight=1 to it and have it take up more screen space.

20. Learn to search your source: The fastest solution to many problems is to find where a particular parameter is used in some other source. Put a copy or link to the sample applications, apps-for-android applications, and any other source you have under one directory tree. Use "grep -ir funky_parameter sample_code/" or your favorite searching routine to quickly find some code that uses that parameter.

21. Eclipse MAT is a great tool to analyze memory allocations. It will help to find memory leaks in your application.

22. If your data set gets relatively large (>100KB) don't store it in text file or XML file - parsing it will be too expensive. Use the native SQLite implementation instead.

23. Starting a new thread is expensive. Use one thread that executes required tasks one by one, or use thread pools. You should use built in classes like AsyncTask whenever possible.

24.  MediaPlayer VS. SoundPool : There is a HUGE difference between MediaPlayer and SoundPool. These Java classes play media (sounds, songs, videos, etc). What I think is odd is that I never knew that SoundPool existed! Yeah, if you casually broswe to the Audio Video section of the Android Developer site, it only mentions MeidaPlayer! If you are making an application that is going to play more than 7 sounds and you use MediaPlayer, it will force close no matter which version of the SDK you are running. In fact, if you create the MediaPlayer objects in the beginning of your main class, you will get a force close. I try to read the fine print in the debugger, but I can’t make heads or tails of it. Playing sounds, like Sound Board applications, video games, etc, should be coded to play sounds using the SoundPool. Sure, it’s like 10 more lines of code you have to reference (use Google), but it won’t force close.