【问题标题】:Multiple Fragments(Views) and Presenters with a single activity (MVP)多个片段(视图)和具有单个活动的演示者(MVP)
【发布时间】:2018-12-03 14:28:18
【问题描述】:
【问题讨论】:
标签:
android
firebase
android-fragments
mvp
android-mvp
【解决方案1】:
在您的场景中,您可以将身份验证模块分解为:
- 模型(网络连接类)
- 查看(活动/介绍屏幕/登录/注册片段)
- Presenter(处理业务逻辑并粘合模型和视图的类)
查看界面:
interface View {
//Show Introduction screen
void onIntro();
//Show User sign in screen
void onUserSignIn();
//Show User sign up screen
void onUserSignUp();
//User logged in i.e. either signed in or signed up
void onLogin();
}
模型接口:
interface Model {
//on Login / Signup successful
void onLogin();
}
演示者界面:
interface Presenter {
// Perform sign in task
void performSignIn();
// Perform sign up task
void performSignUp();
}
Activity 会实现这个 View 并且会实现这些方法:
class AuthenticationActivity extends Activity implement View {
Presenter presenter;
public void onCreate() {
// Initialize Presenter as it has all business logic
presenter = new Presenter(view, model);
}
public void onIntro(){
// initialize a new intro fragment
// and add / replace it in activity
}
public void onUserSignIn(){
// initialize a new sign in fragment
// and add / replace it in activity
}
public void onUserSignUp() {
// initialize a new user sign up fragment
// and add / replace it in activity
}
void onLogin() {
// do what you want to do. User has logged in
}
}
这将为您提供基本概念,例如如何在 Android 中设计登录 MVP 以及流程如何工作。