Skip to main content

Android MVP for Beginners


We have a video player application that we developed as a weekend hobby project which does a few things like slow motion, reverse and zoom. Having done the video listing part of the application with very little emphasis on design and maintainability of the code, soon after adding new features the code became an “embarrassment”. Kept getting crashes and the only solution for those crashes we could come up with was adding a try catch :(.
Needed to make the video listing part of my application crash free, easy to maintain and extend. I had to turn to a design pattern and it was the
MODEL VIEW PRESENTER
.
I have never professionally written code in Android, so all the explanations below would be from a beginners perspective.
This article is implementation of the ideas I learned from the below article. Please go through the below article to better understand the concepts
https://www.techyourchance.com/mvp-mvc-android-1/
Blueprint for MVP Application


blueprint for mvp application
MVP Explained


Credit: https://www.techyourchance.com/
Like most architectural patterns MVP is open to a lot of variety and experimentation and its implementation can be ambiguous. So you can modify your implementation according to your needs as long as your implementation meets the below objectives.
  1. Separate the code for the view, which we show to the user like a list, buttons, labels, textbox etc from the business logic which is triggered on user interaction with the view which we call presenter. The data that we show in the view should be provided by another separate module which we call as model. In turn code becomes readable, understandable and maintainable.
  2. The communication between the view and the model needs to take place through the presenter i.e. The view and model cannot have reference of one another.
  3. Using this design pattern we should be able to make changes in one components without having the need to change any code in the other two components. Also in case we want to completely replace the view or model with another implementation, that should be possible as well. for example you can use content provider to get list of videos, that tomorrow should be replaceable by sqLite database without change in any other part.
  4. Maximize the code that can be tested with automation.
Android Activity And MVP
The android framework makes it really easy to start development of any idea that you have in mind, but this structure of code very soon becomes unmanageable with a lot of spaghetti code and classes with different functionalities all mixed into one activity/fragment class.
Now, the android activity seems to give the functionality of view since we do things like setContentView() in OnCreate(). Activity at the same time maintains the android lifecycle events like onCreate() onResume() etc which would be the business logic i.e. presenter.
Another simple example to show how they are entangled
  1. Register listeners for user’s interactions with application’s UI — View
  2. Perform actions in response to user’s interactions with application’s UI — Business Logic
Most of you would agree that debugging an Activity code with several lines of code registering UI components and at the same time also having a few lines of business logic embedded here and there is a huge pain.
Now, on the basis of Single Responsibility Principle, we need to assign Activity the role of a
View
or a
Presenter.
I decided to use
Activity as the Presenter,
because activity has the
context
(which is the god Object that provide access to most platform’s features). This makes Activity a natural choice for keeping the responsibility for business logic. After all we want keep the view to be dumb module that only has the functionality to load the UI elements. For more detail on this topic, you can go through this article explaining why activities are not UI elements.

Implementation of MVP in my App
The complete code is available on github. Please check out android-video-listing-mvp

Author: Nitin Agarwal
Source: https://android.jlelse.eu/android-mvp-for-beginners-25889c500443

Comments

Popular posts from this blog

What is Blockchain Technology?

A  Blockchain , initially  Block chain , is a developing rundown of records called blocks, which are connected utilizing cryptography. Blockchains which are decipherable by people in general are broadly utilized by cryptocurencies. Private blockchains have been proposed for business utilize. Here, is a short story which will make you comfortable with the  Blockchain Technology . Mario and Luigi Mario needs to send $100 to his brother, Luigi, because Luigi being Luigi, he got into some debts at the other end of the world. Mario walks into the bank and says “I’d like to send $100 to Luigi”. The Cashier says “Account card please”, “ID please”, ”signature” and “done”. In this centralized situation, the bank is the *central* expert over Mario's and Luigi's cash. Both Mario and Luigi trust the bank to exchange the sum, and trust the numbers appeared on their bank account statements. They trust the bank in-spite of the way that all the bank needs to do is change

Android Livedata and MutableLiveData

Open Event Android   Android Livedata and MutableLiveData December 14, 2018 Problem Suppose that we need to perform actions in light of changes in lifecycle status of Android Component. Some of the time you may wanna observe certain qualities being changed over the setup change. We all faced these problems somewhere down the road, trying a lot of different patterns MVP, MVVM, and others. But to implement these patterns is also a big problem for beginners. Solution The good news is that with the Android Architecture Component and more precisely with the lifecycle LiveData and ViewModel , we can easily overcome these problems. LiveData LiveData is an observable data holder. It is also a lifecycle aware. By lifecycle aware I mean, it can only be observed in the context of a lifecycle, more precisely in the context of an Activity or Fragment lifecycle. By passing the reference of an Activity or Fragment , it can understand whether

Why use Snackbar instead of Toasts?

Open Event Android   Why use Snackbar instead of Toasts? January 14, 2019 Problem Toasts are kind of deprecated as any user who has disabled notifications for an app won't be able to see toast as well. The Open Event Android App was using toast for displaying information. So the task is to completely remove toasts and use snackbar instead. Solution Why Snackbar? Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen. The Difference Snackbar Contain a single line of text directly related to the operation performed. They may contain a text action,