Android MVP learning and androidmvp Learning

Source: Internet
Author: User

Android MVP learning and androidmvp Learning

MVP -- Model View Presenter

This article by MartinFowler divides MVPs into SupervisingPresenter and PassiveView


MVP example in Android: https://github.com/antoniolg/androidmvp

Let's take a look at the project structure.



It can be seen that each function module contains XXActivity, XXView (Interface), XXPresenter (interface), XXPresenterImp


LoginView interface for logical processing

public interface LoginView {    public void showProgress();    public void hideProgress();    public void setUsernameError();    public void setPasswordError();    public void navigateToHome();}


LoginPresenter Interface

public interface LoginPresenter {    public void validateCredentials(String username, String password);}

LoginActivity implements the LoginView interface and corresponding methods. It contains a Presenter as the member variable. presenterImp calls the response method to update and synchronize the UI.

public class LoginActivity extends Activity implements LoginView, View.OnClickListener {    private ProgressBar progressBar;    private EditText username;    private EditText password;    private LoginPresenter presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        progressBar = (ProgressBar) findViewById(R.id.progress);        username = (EditText) findViewById(R.id.username);        password = (EditText) findViewById(R.id.password);        findViewById(R.id.button).setOnClickListener(this);        presenter = new LoginPresenterImpl(this);    }    @Override public void showProgress() {        progressBar.setVisibility(View.VISIBLE);    }    @Override public void hideProgress() {        progressBar.setVisibility(View.GONE);    }    @Override public void setUsernameError() {        username.setError(getString(R.string.username_error));    }    @Override public void setPasswordError() {        password.setError(getString(R.string.password_error));    }    @Override public void navigateToHome() {        startActivity(new Intent(this, MainActivity.class));        finish();    }    @Override public void onClick(View v) {        presenter.validateCredentials(username.getText().toString(), password.getText().toString());    }}


LoginPresenterImp, which implements LoginPresenter
ublic class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener {    private LoginView loginView;    private LoginInteractor loginInteractor;    public LoginPresenterImpl(LoginView loginView) {        this.loginView = loginView;        this.loginInteractor = new LoginInteractorImpl();    }    @Override public void validateCredentials(String username, String password) {        loginView.showProgress();        loginInteractor.login(username, password, this);    }    @Override public void onUsernameError() {        loginView.setUsernameError();        loginView.hideProgress();    }    @Override public void onPasswordError() {        loginView.setPasswordError();        loginView.hideProgress();    }    @Override public void onSuccess() {        loginView.navigateToHome();    }}

Throughout the project architecture, the logic processing and UI synchronization updates are separated from the Activity, making the code more concise.

I am not very familiar with MVP. I still need to look at more examples.


MVP improvement: blog






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.