我正在将我的应用程序迁移到MVP。从这个konmik中获取了静态Presenter模式的提示。
这是我简要的MVP策略。为了简洁起见,删除了大部分样板代码和MVP监听器。这种策略帮助我使后台进程适应屏幕方向的变化。与结束活动的暂停相比,活动可以正确地从正常暂停中恢复。此外,Presenter仅具有应用程序上下文,因此不会保留活动上下文。
我不是Java专家,这是我第一次尝试MVP并使用静态Presenter让我感到不舒服。我错过了什么吗?我的应用程序运行良好,并且响应速度更快。
视图
这是我简要的MVP策略。为了简洁起见,删除了大部分样板代码和MVP监听器。这种策略帮助我使后台进程适应屏幕方向的变化。与结束活动的暂停相比,活动可以正确地从正常暂停中恢复。此外,Presenter仅具有应用程序上下文,因此不会保留活动上下文。
我不是Java专家,这是我第一次尝试MVP并使用静态Presenter让我感到不舒服。我错过了什么吗?我的应用程序运行良好,并且响应速度更快。
视图
public class MainActivity extends Activity{
private static Presenter presenter;
protected void onResume() {
if (presenter == null)
presenter = new Presenter(this.getApplicationContext());
presenter.onSetView(this);
presenter.onResume();
}
protected void onPause() {
presenter.onSetView(null);
if(isFinishing())presenter.onPause();
}
}
演示者
public class Presenter {
private MainActivity view;
Context context;
public Model model;
public Presenter(Context context) {
this.context = context;
model = new Model(context);
}
public void onSetView(MainActivity view) {
this.view = view;
}
public void onResume(){
model.resume();
}
public void onPause(){
model.pause();
}
}
模型
public class Model {
public Model(Context context){
this.context = context;
}
public void resume(){
//start data acquisition HandlerThreads
}
public void pause(){
//stop HandlerThreads
}
}