Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Architecture Design - How to do it right?

how does a good architecture for an android app look like? Should all the "work/business logic" been done in a background service and the Activity communicates just with the service to query/fetch data from somewhere (local/distant)?

Would you implement the "service" that the Activity calls as a real Android-service? Or a POJO-Singleton that does the work (perhaps using background threads). Or instantiate background threads in your activity for time-consuming actions (query a webservice).

How do you abstract your data access the right way? Would you use a ContentProvider to access/abstract your data? How/From where should it be queried? Activity? Service? ..?

I've tried to search for a good app architecture design, but I only found how the Android architecture looks like, not how an Android app should look like.

So what's your opinion about that? What components of an Android application should communicate which each other to ensure best extensibility/encapsulation,...?

like image 485
KL4711 Avatar asked Feb 23 '11 16:02

KL4711


People also ask

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.

What are the 4 components of Android architecture?

There are four components, each with a specific role: Room , ViewModel , LiveData , and Lifecycle . All of those parts have their own responsibilities, and they work together to create a solid architecture.

What are the 5 sections of the Android architecture?

Now, we will start with Android Architecture, it comprises of five levels, which are the Linux kernel, Libraries, Application framework, Android runtime, and System applications.


1 Answers

There's no one answer to this question. Good OO design isn't Android specific. I'd say that the rule is - if the framework gives you a high level object (such as Service in the case of Android) that fits your use case, use it. If you find yourself making POJO implementations of the same things you get for free with the framework, go with the framework.

As far as separation of concerns, this is standard OO stuff. Don't put anything in your Activity classes that isn't the job of the Activity. Over-stuffing the Activity with methods and properties that the Activity needs but aren't really the job of the Activity is bad - makes the intention of your Activity hard to understand.

I usually separate stuff into sub-packages in my apps.

  • com.myname.myproject.app - base classes, global application functionality
  • com.myname.myproject.net - network stuff, network related utils
  • com.myname.myproject.data - db helpers, providers, etc
  • com.myname.myproject.model - object model

etc.

As far as communication within your app...

I always have a custom Application class that I register in the manifest. This way, when I have controllers and helpers that need to be a "single instance", I don't have to do all that crazy thread safe singleton stuff...I just keep one global copy of.

RoboGuice is a dependency injection framework that makes this even easier to accomplish...definitely worth looking into. If this interests you, the Google Group for RoboGuice is great and is constantly filled with the creators of the framework who can basically answer anything you need.

As far as in-app communication, I use my single instance Controller and State classes to hold state and do common tasks, and I usually use BroadcastIntents to communicate back to Activities from Services

like image 103
Rich Avatar answered Oct 19 '22 14:10

Rich