Android Developer

Los Angeles Freelance Developer

Android Developer header image 2

Android Architecture: Part 1, Intro

November 24th, 2011 · 11 Comments · Android, OOP

As an Actionscript developer and having worked on numerous data-driven applications, I’ve had an Object Oriented mindset for many years now. When I first started learning Android I want to know some of the common approaches people were taking to their applications. I had questions like “What approach are people using to send data back and forth using web services?” and “What does an MVC look like in Android?” and “How are people persisting their data locally?” and “What are some of the common patterns in Android to achieve common tasks?”

Sleuth:
I began investigating what MVC looked like in Android. After spending a lot of time reading community forums like Stack Overflow and Android blogs, I frequently saw the term “View-Controller” when talking about implementing an MVC pattern. Summarizing, people were saying the Activity should function both as both the View and Controller. This idea didn’t sit with me too well. The idea of a View-Controller violates the Single Responsibility Principle. Let’s not start mixing and matching the responsibilities.

I then went on to investigate how other developers were structuring their code for handling web services. So I downloaded a popular online dating app since I knew it would be heavily web-service driven, decompiled it using dex2jar (maybe I’ll make a blog post on this process sometime), and took a look at their source code. Their approach was to make all request go through a single class. For instance, XRequest.viewUser(142) and XRequest.sendMessage(142, “hello world”) and XRequest.blockUser(142, 138). You get the idea. Again, this approach didn’t sit well with me. Not only did this approach violate the Single Responsibility Principle it also violates the Open-Closed Principle. If a new API call was to be added, they would have to go back in XRequest class and modify the code. The class was not closed to modifications and was not open to extensibility.

The Results:
Through my reading and research, I didn’t feel like I’d saw a solid or common architectural approach on Android. This series of articles will walk through building a simple application from start to finish using practices I’ve found to be very beneficial and extensible. We will explore using a strict MVC in Android, use of the State Pattern, Data Access Objects for persisting data, and Commands for sending and receiving data using a web service. The series will cover how to implement these ideas in Android and not necessarily go in depth as to why or what benefits these patterns have. I’m going to assume you know already know the benefits of MVC and such. Additionally, the order I present the topics is not the order which I would code an application but the order I find makes most sense in disussion.

Our Project:
We will use an application I wrote called “Tap Counter” for the discussion. Go ahead and grab the sources, compile it and play around with it. It’s one of those tools gate attendants use to count how many people pass by. Here’s a couple screenshots because it’s much more fun reading about code when you get to see the results it produces.
Sources

Tags: ··

11 responses so far ↓

  • 1 James // Jan 9, 2012 at 4:26 am

    nice kick starter

  • 2 Daniel // Jan 17, 2012 at 5:08 pm

    Thanks for the useful series of posts!

    I found the app lifecycle a little unusual, in that the activity stack could accumulate multiple instances of TapActivity.

    To demonstrate, launch TapCounter, then open a previously saved tap count, then open another tap count, etc. Now hit back button to traverse the (possibly very long!) activity stack backward.

    In TapListActivity, I expected the back button to send me to the TapActivity. In TapActivity, I expected the back button to send me back to the home screen (or another activity on the stack that is not TapActivity). To implement this behavior, I added one flag to the TapActivity Intent to ensure only one TapActivity instance is on the stack. Here’s a short excerpt from TapListActivity.onCreate:


    Intent intent = new Intent(TapListActivity.this, TapActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Documentation: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

  • 3 Video: Android Stylus-Based Weekly Planner for TPT » Mark Lapasa // Feb 2, 2012 at 1:56 pm

    [...] in itself. This app would not have been possible if it wasn’t for Joshua Musselwhite’s Android MVC Walkthrough and Volker Braun’s Quill Source code. Thanks guys for helping lower the already high learning [...]

  • 4 Shiki // Mar 13, 2012 at 4:24 pm

    Thanks a lot for this amazing tutorial ! It’s the best android tutorial I found for now.

  • 5 Samus Arin // May 18, 2012 at 8:51 am

    Hello.

    I was halfway through porting your code to C#/.NET to use as Template for an MVC architecture.

    However, I discovered MonoCross, which is an MVC design pattern for developing cross-platform apps for mobile devices in Mono/.NET
    (MonoCross is manifested as visual studio templates, as well as MonoDevelop templates for Mac/IOS projects).

    I know that this post is for Android development in particular, however if anyone is using Mono I highly recommend implementing the MonoCross pattern).

  • 6 Dave // Jul 10, 2012 at 9:41 pm

    Hello,
    Great Blog. Very useful (so far)

    I’m trying to download the source code. When I try to unzip it, a password is required.

    What’s the password?

    Thanks

  • 7 musselwhizzle // Jul 10, 2012 at 9:46 pm

    Hi Dave, Thanks! There is no password. I’m on a MAC if that matters. I just downloaded and unzipped it and everything worked fine for me. I don’t think other people were having trouble. Perhaps you could try winzip or winrar.
    Cheers
    Josh

  • 8 Atif // Sep 24, 2012 at 4:11 am

    good information indeed, em planning to switch to android from C#

  • 9 cameo // Sep 24, 2012 at 5:07 am

    Hello,
    Thanks for the great blog! but the sources file is broken. I cannot unzip it successfully. Could you please check it?

  • 10 Comparing iOS and Android « Multimedia Dev Blog [B-KUL-G0Q55A] // Jan 21, 2013 at 7:56 am

    [...] J. Musselwhite. Android architecture: Part 1, intro – android developer. URL: http://www.therealjoshua.com/2011/11/android-architecture-part-1-intro/, 2011. [Online; accessed [...]

  • 11 Dormeo Ergo Sum | Lesson learnt: UI and program logic decoupling // May 5, 2013 at 11:29 pm

    [...] class design. It is a little similar to what I suggest before except more buried in theory. This guide also gives a step by step way of implementing MVC on Android. I hope this advice helps with Android [...]

Leave a Comment

Powered by sweet Captcha