A blog for technology, SEO tips, website development and open source programming.

Android Developer Tips

0 665

  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.

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More