When you start learning coding to be a software developer, website developer, Android or iOS developer, your coding journey begins with simple programs for adding or subtracting two numbers, leap year, fibonacci series and then you move onto some small real world applications such as banking application with the three basic operations for balance enquiry, deposit and withdrawal.

Collecting user input, storing the input values in variables, defining methods/functions with proper validations and displaying the output, everything is covered in a single program.

However, desktop apps, web apps and mobile phone apps used by businesses are much more complex. Developing optimized apps easily is not possible without separating different layers of code. You cannot manage it if your code is not modular. It becomes difficult to update the app or add new features. This is the reason why you need an architecture for Android app development.

If you are a beginner, you should start with the MVP architecture.

MVP Architecture


MVP (model-view-presenter) is an architectural pattern that promotes separation of concerns and facilitates unit testing. This architecture has the following three components:

 

  1.  Model (interactor, REST API, database API)
  2. Presenter
  3. View (activity, fragment, view)

Model

It is the data store of the app. The model layer includes Shared Preferences, SQLite databases, and repositories for network calls. Model provides the data to be displayed in your app. API calls in the activity class are no longer needed. They are separated in the model and results can be retrieved from there.

 

View

It consists of fragments, activities and other views. The view is what you see on your screen. The data come from the presenter and populated into the views on the screen. 

 

Presenter

The presenter makes API calls, fetches data from files or databases, replaces fragments, fetches data from sharedpreferences, starts new activities and performs other similar tasks. The presenter contains the business logic. 

The following are the features of MVP architecture:

  • Separation of concerns
  • Easier to debug
  • Reusability
  • Unit/integration testing
  • Scalability

Now, let’s see the implementation of MVP architecture with an Android app using Dagger2, RxJava, GreenDao, PlaceHolderView, FastAndroidNetworking and AndroidDebugDatabase.


Architecture Blueprint
 

ArchitectureBlueprint

Project Structure

ProjectStructure

The app has the following packages:

  • data package contains all the components that access and manipulate data.
  • di package contains classes that provide dependency using Dagger2
  • ui package contains view classes and corresponding presenters
  • service package contains services for the app.
  • utils package contains utility classes.

Classes in the app use key OOP features like inheritance and facilitate code reusability.

Library reference resources

Dagger2

https://github.com/MindorksOpenSource/android-dagger2-examplehttps://github.com/MindorksOpenSource/android-dagger2-example

RxJava2

https://github.com/amitshekhariitbhu/RxJava2-Android-Samples

ButterKnife

http://jakewharton.github.io/butterknife/ 

FastAndroidNetworking

https://github.com/amitshekhariitbhu/Fast-Android-Networking 

GreenDao

http://greenrobot.org/greendao/ 

PlaceHolderView

https://github.com/janishar/PlaceHolderView 

Calligraphy

https://github.com/chrisjenx/Calligraphy 

AndroidDebugDatabase

https://github.com/amitshekhariitbhu/Android-Debug-Database 

Concept reference resources: 

 

  1. Introduction to Dagger 2: Part 1
  2. Introduction to Dagger 2: Part 2
  3. Android Dagger2: Critical things to know before you implement
  4. The Best Android Networking Library for Fast and Easy Networking
  5. RxJava + Fast Android Networking
  6. Migrating from RxJava 1.0 to RxJava 2.0 and Learn RxJava by Examples
  7. Android Tinder Swipe View Example
  8. Debugging Android Databases And Shared Preferences In The Easiest Way
  9. RxJava Anatomy: What is RxJava, how RxJava is designed, and how RxJava works.
  10. Powerful Android ORM: greenDAO 3 Tutorial


MVP template
 

When you are following architecture patterns like MVP, MVP clean or MVVM, this involves creation of basic files like API models, View, Presenter, and Android Activity and then boiler plate code. You have to spend 1-2 hours on each screen. You can use an automated template to save time and ease out work.

 

 

How to Install 

Find and paste the MVPActivity folder under the root directory of android-mvp-architecture app at the below location:

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities 

Restart the Android Studio. 

AndroidStudio

How to Use

Select the folder in which you want to create the new MVP folder. The following will be created in the new folder: 

  • View class
  • MVpPresenter 
  • Presenter 
  • Activity

 

How to use template

Related posts


What’s New in Bootstrap 5 Alpha
AngularMVCArchitecture
Angular MVC Architecture: Angular App
ASimpleGuidetoBasicGITCommands
A Simple Guide to Basic GIT Commands