
- Be resolution independent. Check out “layoutopt” and “hierarchyviewer” that come with Android SDK under “tools”. They help analyzing and optimizing layouts.
- Respect and use an application lifecycle.
- 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.
- 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.
- Always check whether “network connection” is enabled on a device, before attempting to transfer data.
- 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.
- 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.
- 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.
- Think of hiring or delegating UI to people who are designers. Beauty is important.
- Consider using a “non sticky” services when appropriate.
- 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.
- 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 )
- Kill your own services via stopSelf()
- Don’t use undocumented ( not officially supported ) APIs => Next Android release your app is going to break.
- Use Intents and Intent Filters to Leverage Other Apps.
- Prefer Alarms and Intent Receivers to Services.
- Make use of onPause()/onResume to save or close what does not need to be opened the whole time.
- 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.
- 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.
- 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.
- Eclipse MAT is a great tool to analyze memory allocations. It will help to find memory leaks in your application.
- 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.
- 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.