I am new in android development.I want to implement MVP in my project but I don't know What are the advantages of using MVP SO Please tell me what are the advantages of using MVP in android.
There are two parts to your question as I see it:
The general advantages that one gets from implementing MVP (or similar architectural pattern like MVC, MVVM, MVVC, etc.) are:
The above are general advantages of MVx on any platform. These also apply to Android, but, IMHO, in Android the gains from following MVx are higher due to the following reasons:
If you want to go down MVP path in Android, check out these resources as well:
Technically speaking, the advantages are testability, maintainability, extendability, etc, as others noted. I have written a complete post on that here.
But as you said you are new to android, I feel a tangible example of using an architectural pattern in building an app will help you much better understand its importance:
Assume an app having an EditText
field where you enter an address and a FIND button leading to a search results page.
If you don’t use MVP or any other proper architecture, you have to put everything inside your Activity
or Fragment
:
findButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callTheNetworkSearchMethod(); }
});
Now what if you want to change the endpoint API of your search address? This way you have to change that Activity
for everything, even for the endpoint API. But is it reasonable? Definitely not.
In a team project, when the lead developer reviews the feature you have developed she finds out that the whole Activity
is changed for just adding a single endpoint api change. She has to go through the scan the whole Activity
just to figure out you have only changed an endpoint. Also, if another developer is working on doing a minor change to the Activity
, you might even get conflicts and have to spend some time on resolving it.
All of the above and many other enhancements are at your reach by using MVP, in which you simply write:
findButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPresenter.doWhenFindButtonIsClicked(); }
});
Now, if you want to change anything when the click is made on FIND button you would do it in the Presenter and the View remains intact.
This is one of the most important aspects of using architectural patterns, called ‘separation of concerns’.
If you’re interested to know more about how MVP can help with an android project, together with a complete sample app, check my MVP article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With