首页 文章

多个片段(视图)和具有单个活动的演示者(MVP)

提问于
浏览
0

我正在使用MVP模式开发Android应用程序,该模式使用Firebase服务和Firebase身份验证 .

在认证模块中,我有三个片段(视图) - a)介绍屏幕片段,b)登录片段和c)注册片段 . 每个片段都有自己的演示者 .

当用户单击介绍屏幕中的“登录”按钮时,如何调用SignIn Fragment并实例化其演示者和模型?

根据Android体系结构示例 - https://github.com/googlesamples/android-architecture,片段(视图)和演示者在活动中实例化,但示例未显示如何在一个身份验证活动中处理多个片段 .

我在堆栈溢出时发现了一个类似的问题 - (Implementing MVP on a single activity with two (or multiple) fragments),但找不到满意的答案 .

我是Android MVP的新手所以请帮助我,谢谢 .

1 回答

  • 0

    在您的方案中,您可以将身份验证模块中断为:

    • 型号(网络连接类)

    • 查看(活动/介绍屏幕/登录/注册片段)

    • Presenter(处理业务逻辑并粘合模型和视图的类)

    View Interface:

    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();
    }
    

    Model Interface:

    interface Model {
        //on Login / Signup successful
        void onLogin();
    }
    

    Presenter Interface:

    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以及流程如何工作 .

相关问题