Android---MVP学习之路(二)

通过 Android---MVP学习之路(一) ,我们对mvp有大体上的认知。但是还是有问题的。比如页面登录的过程中,我退出这个activity,然后presenter登录回调完成后,再去响应activity中的方法。acitivity都没了,还怎么会响应,会造成空指针异常。现在就来解决这个问题。

思路:如果activity退出后,我们应该把presenter与acitvity解除关系。这样子presenter就不会响应activity中的方法。我们把这个称为解绑,有解绑就会有绑定这个操作。所以,我们通过写一个接口,来声明这两个操作。

因为这两个操作,对于所有的presenter都会有。所以我们对共性代码进行抽取,之后对整个项目的都会慢慢抽取共性。

先看下按照下列步骤操作后的项目目录结构:


Android---MVP学习之路(二)_第1张图片
图片.png
步骤1:先抽取View的公共方法

public interface BaseMVPViewInterface {

    public void showLoading();

    public void hideLoading();

    public void showFailError(String message);
}

修改LoginViewInterface,改成继承 BaseMVPViewInterface

public interface LoginViewInterface extends BaseMVPViewInterface {

    public String getUserName();

    public String getUserPassword();

    public void clearName();

    public void clearPassword();

    public void go2OtherActivity();

}

步骤2:创建BaseMVPPresenterInterface
public interface BaseMVPPresenterInterface {

    public void attachView(V view);

    public void detachView();
}
步骤3:创建BaseMVPPresenterImp来实现BaseMVPPresenterInterface
public class BaseMVPPresenterImp implements BaseMVPPresenterInterface {

    private V view;

    @Override
    public void attachView(V view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        this.view = null;
    }

    public boolean isViewAttached() {
        return view != null;
    }

    public V getAttachView() {
        return view;
    }
}
步骤4:修改LoginPresenter来继承BaseMVPPresenterImp
public class LoginPresenter extends BaseMVPPresenterImp {

    private Handler handler = new Handler(Looper.getMainLooper());
    private LoginBizImp loginBizImp;

    public LoginPresenter() {
        this.loginBizImp = new LoginBizImp();
    }

    public void login() {

        if (!isViewAttached()) {
            return;
        }
        getAttachView().showLoading();
        loginBizImp.login(getAttachView().getUserName(), getAttachView().getUserPassword(), new OnLoginListener() {
            @Override
            public void success() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (!isViewAttached()) {
                            return;
                        }
                        getAttachView().hideLoading();
                        getAttachView().go2OtherActivity();
                    }
                });
            }

            @Override
            public void failure() {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (!isViewAttached()) {
                            return;
                        }
                        getAttachView().showFailError("登录失败");
                        getAttachView().hideLoading();
                    }
                });
            }
        });
    }

    public void clear() {
        if (!isViewAttached()) {
            return;
        }
        getAttachView().clearName();
        getAttachView().clearPassword();
    }

}
步骤5:创建BaseMVPAcitviy来继承你的BaseActivity并且抽取公共代码
public class BaseMVPActivity extends HPDBaseActivity implements BaseMVPViewInterface {

    protected ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressDialog = new ProgressDialog(this);
    }

    @Override
    public void showLoading() {

        progressDialog.setMessage("showLoading");
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    @Override
    public void hideLoading() {

        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void showFailError(String message) {
        if (TextUtils.isEmpty(message)) {
            message = "发生错误";
        }
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
步骤6:修改LoginActivity
public class LoginActivity extends BaseMVPActivity implements LoginViewInterface {

    private EditText etName, etPassword;
    private Button btnLogin, btnClear;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        loginPresenter = new LoginPresenter();
        loginPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.detachView();
    }

    private void initView() {

        etName = findViewById(R.id.et_name);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        btnClear = findViewById(R.id.btn_clear);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.login();
            }
        });

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.clear();
            }
        });

    }

    @Override
    public String getUserName() {
        return etName.getText().toString().trim();
    }

    @Override
    public String getUserPassword() {
        return etPassword.getText().toString().trim();
    }

    @Override
    public void clearName() {
        etName.setText("");
    }

    @Override
    public void clearPassword() {
        etPassword.setText("");
    }

    @Override
    public void go2OtherActivity() {
        Toast.makeText(this, "go2OtherActivity", Toast.LENGTH_SHORT).show();
    }

}

好了,mvp的attachview和detachView很好解决了问题。现在还有一个问题,如果手机屏幕旋转等,触发了activity的重启,该怎么处理Presenter呢?请看 Andriod---MVP学习之路(三)

你可能感兴趣的:(Android---MVP学习之路(二))