Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application architecture - what is the suggested model? [closed]

In the same way a web or desktop app might have three or n tiers - UI, Business, Data for example - what is the suggested structure for an Android application? How do you group classes together, what layers do you have etc?

I'm just starting Android dev (an internet-based app that must respond to incoming notifications) and have no real feel for the structure I'm aiming at. Suggestions appreciated.

like image 200
MalcomTucker Avatar asked Jul 23 '10 17:07

MalcomTucker


People also ask

Which method is called when app is closed?

The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.

When closing an app on Android what is happening in the backend?

In general, there's no such thing as closing applications in Android: the user just stops using the app. It's up to the programmer to make sure that the user does not mention process creation and termination.

Which architecture is best for Android?

The most popular android architectures used by developers are the following: MVC (Model — View — Controller) MVP (Model — View — Presenter) MVVM (Model — View — ViewModel)


2 Answers

IMHO, Android "wants to" follow a MVC pattern, but view & controller are generally really coupled in activities.

It makes unit test harder and it's hard to obey to the Single Responsibility Principle.

I found a really nice Android architecture presented here, there could be an idea. Everything is loosely coupled, so much easier to test and edit.

Obviously, I'm sure there are a lot of others possibilities (like the MVP pattern (Model View Presenter) - and here are answers talking about MVP in Android), but you should still take a look on it.

like image 188
louiscoquio Avatar answered Oct 07 '22 12:10

louiscoquio


I've been working on Android for 9 months now from a server-side background where full unit testing and layered architectures are common and work well.

Through lots of trial and error and I would strongly suggest using the Model View Presenter pattern, not Model View Controller.

A huge issue I've found is that Activities/Fragments have a lifecycle which is outside your control and can lead to unexpected issues.

For example, our main android app wants to be used in landscape mode on tablets. We do this in OnCreateView() or OnCreate().

On a Nexus 7, the default view is portrait so what happens is that it starts the activity in portrait mode, our code then says go to landscape and android ultimately creates the activity class 3 times!

We've hooked up network requests to onCreate and they end up happening 3 times in this case.

Sure, we can add logic to look for duplicate calls but, in my opinion, it would be better, architecturally to try and divide the UI from the business logic.

My recommendation would be to use the factory pattern to create presenters from the activity but make sure the factory only ever returns the same instance. The presenter can then contain logic to do network request, look for duplicates and return cached results and general business logic.

When results from network calls return, either post to a bus such as Otto which the activity (register for the event on onResume() and deregister during onPause()) has registered to, or make sure the callback interface implemented by the activity has been updated to the last activity in the presenter.

This way, code in the presenter downwards is unit testable and not reliant on flaky UI layer testing.

like image 39
FinalFive Avatar answered Oct 07 '22 13:10

FinalFive