Android coding best practices/design patterns

Lennard Fonteijn picture Lennard Fonteijn · Mar 26, 2012 · Viewed 23.5k times · Source

In a recent question I asked I was directed to this website: http://developer.android.com/design/index.html

Amazing site, but it didn't answer one particular question: What are the best practices/design patterns to apply in the design of an application code-wise?

I did lookups for MVC/MVP, etcetera, and while that yields results, it's only about the actual implementation of said patterns, rather than other available options and such.

I tried decompiling and analyzing various apps Android installs by default, like the Market, but I couldn't really find a structure in Google's code. Does anyone have tips on how to setup Android apps in such a way they are maintainable, extendable, etc. I am aware of the wide meaning of these words and that they are purely subjective to the programmer for that matter, but I can't express it any differently.

One best practice I already encountered is one view per Activity and having lots of Activities in the app for the backstack to work properly, but other than that, I have no clue how to actually setup the Activity itself.

Answer

Jaap van Hengstum picture Jaap van Hengstum · Jul 18, 2014

First of all, read the API Guides in particular the parts on Activities and Fragments. But if you've got the time, read all the API guides, they are a really great resource for understanding Android development.

Depending on the Android devices you want to support, I would recommend using the v4 Support Library and the v7 Appcompat Library. The first one (v4 Support Library) I use always because of the support for nested fragments (getChildFragmentManager() - not supported natively on < API 18) and the ViewPager. The Appcompat libary is mainly for supporting the Actionbar on devices with Android versions below 4.0.

I would also study Gradle as your build system and [http://developer.android.com/sdk/installing/studio.html](Android Studio) (free) or IntelliJ IDEA (commercial) as your IDE.

Considering third party libraries, the library stack I see mostly used these days (and use myself for the most part) are:

  • Guava as a general Java helper library.
  • Dagger and ButterKnife for dependency injection.
  • OkHttp as the HTTP transport library.
  • RetroFit or Volley as REST libraries.
  • Picasso as an image loading library. Having a good image loading library when you need to download/display images from the server is really important in Android because it takes care of memory handling and caching, which can be really hard if you try to do it all by yourself.

Alternatives that are also used a lot are:

Other libraries I use and recommend:

Chris Banes (a wellknown Android developer) recently released the source code of one of his applications which you can use as an example of how to build an Android app using several of the libraries mentioned above.