What Is MVP And What Does It Have To Do With Android
Posted By : Rahul Baboria | 28-Jan-2018
Introduction:
The Android default templates lead to the creation of large activities or fragments in a single project. There is UI part and logic part and these classes contain both of them. It makes harder to test and maintain the application with large no of classes and XML files. MVP is a most popular architecture which divides application development into three parts, thus it makes easy to maintain the application.
view :
View comprises of visual part of the application in MVP. It consists of UI part only, logical part is not defined here.We have created a new interface for interaction between view and presenter. The presenter uses these interface methods to manipulate the view. Example method names would be showAlertDialog, updateData.
Presenter:
The presenter contains all the logic stuff and makes the view to update changes. Presenter interacts with the model, fetches and transforms data from the model in order to update the view.There shouldn't be any dependency on Android SDK in presenter if possible.
The model :
A model contains a data provider and lines of code to fetch and update the data. This is the part of MVP that updates the database, communicate with a web server via services.
MVC vs MVP:
If we compare Model View Presenter to Model View Controller, In the MVP pattern, the views are more separated from the model. The presenter is the only medium that communicates between view and model. It is based on one to one mapping between view and Presenter thus makes it easier to unit test the application, but we can also use multiple presenters for complex views. In the MVC pattern the controllers are behavior based and can share multiple views and View can communicate directly with the model. MVP is currently one of the patterns that the Android community prefers.
Implementation:
Presenter class:
public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {
private V mMvpView;
@Override
public void attachView(V mMvpView) {
mMvpView = mMvpView;
}
@Override
public void detachView() {
mMvpView = null;
}
public boolean isViewAttached() {
return mMvpView != null;
}
public V getMvpView() {
return mMvpView;
}
public void checkViewAttached() {
if (!isViewAttached()) throw new MvpViewNotAttachedException();
}
public static class ViewNotAttachedException extends RuntimeException {
public ViewNotAttachedException() {
super("Attach view before any other operation "
);
}
}
}
Here MvpPresenter is an interface which has its two methods that will be called on Activity attach view and on detach view.
public interface MvpPresenter<V extends MvpView> {
void attachView(V mvpView);
void detachView();
}
In order to further create an activity, we have to extend it via BaseActivity where we can keep our basic methods which will be used throughout the project such as showing progress dialog, show alert dialog, showing toast, etc. I'm keeping just progress dialog method.
public abstract class BaseActivity extends AppCompatActivity implements MvpView {
private ActivityComponent mActivityComponent;
private ProgressDialog progressDialog;
public ActivityComponent activityComponent() {
if (mActivityComponent == null) {
mActivityComponent = DaggerActivityComponent.builder()
.activityModule(new ActivityModule(this))
.applicationComponent(MyApplication.get(this).getComponent())
.build();
}
return mActivityComponent;
}
@TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode);
}
}
@TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
@Override
public void showLoading() {
hideLoading();
progressDialog = DialogUtils.showLoadingDialog(this);
}
@Override
public void hideLoading() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.cancel();
}
}
}
There is one more interface i.e MvpView which contains methods such as showLoading, hideLoading, etc.
public interface MvpView {
void showLoading();
void hideLoading();
}
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Baboria
Rahul Baboria is having good knowledge over Android Application.