Where should I do activity navigation in MVP architecture?
Asked Answered
K

1

5

I have an Android project which was built using the MVP architecture. One thing that I'm not quite sure of is that when I want to navigate to another activity after clicking on a button for example, should I place

startActivity(intent) 

inside the view component or inside the presenter component?

Kalinin answered 8/3, 2018 at 2:56 Comment(1)
You should provide a code sample here to illustrate your specific pattern. Which components actually implement the presenter and view interfaces?Reitman
R
7

You have to do this in view component all the ui related activities must be performed in views like open new activity or replacing fragment and asking for permissions.

Let me explain you further suppose we have a single button in our activity so we will write its logic in presenter i.e call a presenter method buttonClick() and if we want to open another activity after our logic we simply call getMvp.openMainActivity(); where as openMainActivity()will be the part of view.

Here is a example methods for basic login activity based on MVP architecture. My Login Presenter MVP interface have

  void onServerLoginClick(String email, String password);

My Login View MVP interface have

void openMainActivity();

My Login Presenter implementation is

@Override
    public void onServerLoginClick(String email, String password) {
        //validate email and password
        if (email == null || email.isEmpty()) {
            getMvpView().onError(R.string.empty_email);
            return;
        }
        if (!CommonUtils.isEmailValid(email)) {
            getMvpView().onError(R.string.invalid_email);
            return;
        }
        if (password == null || password.isEmpty()) {
            getMvpView().onError(R.string.empty_password);
            return;
        }
        getMvpView().showLoading();


        User u   = getDataManager().matchUser(new User(2l,"",email,password,"",""));
        if (u==null){
            getMvpView().onError("No user found");
            getMvpView().hideLoading();


        }else {
            getDataManager().updateUserInfo(
                    "",
                    u.getId(),
                    DataManager.LoggedInMode.LOGGED_IN_MODE_SERVER,
                    u.getName(),
                    u.getEmail(),
                    "",
                    password);
            getMvpView().hideLoading();
            getMvpView().openMainActivity();
        }




    }

my Login View class implementations is

@Override
    public void openMainActivity() {
        Intent intent = MainActivity.getStartIntent(LoginActivity.this);
        startActivity(intent);
        finish();
    }

Hope this will help you to understand the concept.

Remunerate answered 8/3, 2018 at 3:0 Comment(3)
Also, a Context is required both to construct your Intent and start an Activity with it, and it's not something you want your presenter to be passed explicitly if you can avoid it.Bunni
yes definetly you must not open new activity from presenter.Remunerate
This answer is based on the assumption that an Activity is the thing that implements the view interface. Some people think the Activity should implement the presenter interface. Personally I'm not a fan of that but you should be clear. The TL;DR; Only an Activity can do all those things (start other activities, handle runtime permissions) so the answer is "whichever part of your pattern is implemented by the Activity (or Fragment)"Reitman

© 2022 - 2024 — McMap. All rights reserved.