Architecture in Android

Anubhav Malik
2 min readDec 14, 2018

--

Now, what is architecture… exactly?

Not a framework, not a library, it’s a pattern of implementation that you use in your code to make it more clean, structured and testable. Yes, just this.

What is wrong if I don’t use one?

Well, remember when you created your first Android app. Adding everything in your activity. The business logic, the code for manipulating the view, network calls. Actually that doesn’t belong here.

Imagine, you have a chef at your home. His duty is to cook food. You have a domestic help who picks your grocery, and you are paying both of them.

Now, you tell the help what to pick up from the grocery store, he gets it from the store and the chef cooks it. This is exactly how this is expected to work, right?

What if, the chef decides what will be cooked and goes to the grocery store to pickup stuff, the help cooks it.

I know right!

That’s exactly what you’ve been doing with the activities and fragments. Putting network calls and database queries in the activity or your fragment. See, it doesn’t belong there. Activity or a fragment contains all your views they shouldn’t be concerned about what data you’re getting and from where you’re getting it.

So, how does having an architecture help that?

Well, for starters. Your chef’s just gonna cook the food and your help’s gonna just pickup your grocery (and other things you expect from him).

Think of your activity or fragment as your cook, and let’s say the domestic help here is an actual helper (presenter or controller) that you’ll make.

Of course, they need to have some contracts right? Which mentions what all things they’re supposed to do. We can do that using interfaces.

Just realised importance of interfaces apart from handling clicks in recycler view, huh?

Your view is going to have some sort of contract and be bound by it, depending on the implementation and design pattern, you could divide the responsibilities of handling both of them via a Controller ( in MVC), a Presenter (in MVP) or via the view model in MVVM implementation.

We’ll be taking a good look over that soon. I’ll add a demo of all three types we broadly use in Android and share code on Github too.

Till then, Sayonara.

--

--

Anubhav Malik

Google Certified Android Developer | Freelancer | Mentor