0
votes

I'm developing an Android application using the MVP pattern which uses Firebase Services and Firebase Authentication.

In the authentication module, I have three fragments(views) - a) Intro Screen fragment, b) Sign In Fragment and c) Sign Up Fragment. Each fragment has its own presenter.

When the user clicks the Sign-In button in the intro screen, how do I call the SignIn Fragment and instantiate its presenter and model?

As per the Android Architecture examples - https://github.com/googlesamples/android-architecture, the fragment(view) and presenter are instantiated in the activity but the examples do not show how to handle multiple fragments in one authentication activity.

I found a similar question-(Implementing MVP on a single activity with two (or multiple) fragments) on stack overflow but could not find a satisfactory answer.

I am new to Android MVP so please help me, Thanks.

1

1 Answers

0
votes

In your scenario you can break your Authentication module as:

  1. Model (Network Connection Class)
  2. View (Activity / Intro Screen / Sign In / Sign Up Fragment)
  3. Presenter (Class which handles Business Logic and glues Model and View)

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 will implement this View and will implement these methods:

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
    }
}

This will give you basic idea like how to design Login MVP in Android and how flow works.