【问题标题】:Multiple Fragments(Views) and Presenters with a single activity (MVP)多个片段(视图)和具有单个活动的演示者(MVP)
【发布时间】:2018-12-03 14:28:18
【问题描述】:

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

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

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

根据 Android 架构示例 - https://github.com/googlesamples/android-architecture,fragment(view) 和 Presenter 在 Activity 中被实例化,但这些示例没有显示如何在一个身份验证 Activity 中处理多个 Fragment。

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

我是 Android MVP 新手,请帮助我,谢谢。

【问题讨论】:

    标签: android firebase android-fragments mvp android-mvp


    【解决方案1】:

    在您的场景中,您可以将身份验证模块分解为:

    1. 模型(网络连接类)
    2. 查看(活动/介绍屏幕/登录/注册片段)
    3. 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 以及流程如何工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2016-03-19
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多