Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android coding best practices/design patterns [closed]

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.

like image 314
Lennard Fonteijn Avatar asked Mar 26 '12 08:03

Lennard Fonteijn


People also ask

When should design patterns not be used?

If a problem has two solutions, one that fits in ten lines of code, and another one with hundreds of lines of code along with a pattern, please consider not using the pattern. Their presence isn't a quality measurement.

What is the best approach in design patterns in coding?

One of the most popular design patterns used by software developers is a factory method. It is a creational pattern that helps create an object without the user getting exposed to creational logic. The only problem with a factory method is it relies on the concrete component.

Which architecture pattern is best for Android?

The Model—View—ViewModel (MVVM) Pattern While releasing the Android Architecture Components, the Android team recommended this architecture pattern.


2 Answers

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:

  • RoboGuice for dependency injection.
  • Universal Image Loader for image loading.

Other libraries I use and recommend:

  • CommonsWare Android Components, in particular cwac-adapter and cwac-camera. The last one is for using the camera within your app (instead of using an Intent), which can also be really hard to do yourself.
  • HoloAccent, which makes it really easy to style your Holo themed android app.

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.

like image 144
Jaap van Hengstum Avatar answered Oct 03 '22 21:10

Jaap van Hengstum


The first thing that you got to learn is about Android activities really really well. Grok the whole thing. It'll be much easier to go forward from there. Do not go into Java design patterns so early as you would end up trying to fit the problem to your pattern which doesn't end well. Go through the examples on the Android developer website and then write as much code as you can.

I just put together this website called Android App Patterns - http://android-app-patterns.com and it showcases the different UI elements and interaction patterns some popular apps adhere to on Android.

There are a number of libraries which help you implement the above patterns easily. For ex.:

  1. GreenDroid - https://github.com/cyrilmottier/GreenDroid
  2. ActionBarSherlock - https://github.com/JakeWharton/ActionBarSherlock
  3. Android View Badger - https://github.com/jgilfelt/android-viewbadger - Will be used for notifications
  4. NineOldAndroids - https://github.com/JakeWharton/NineOldAndroids - Use HoneyComb's animation API all the way back to version 1.0
  5. Ignition: https://github.com/kaeppler/ignition - Lots of helpers for common Android stuff
like image 33
Abhinav Avatar answered Oct 03 '22 23:10

Abhinav