Android: Create new instance of bundle and pass to intent in dagger in mvp

356 views Asked by At

I would like to use MVP with dagger in my project. In view I have this method and into this method I will pass some object to the presenter:

@Override
public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
    super.onLogin(user, cookie, sessionId, permissions);
    presenter.onLogin(user, cookie, sessionId, permissions);
}

Here is my presenter:

public class Presenter implements ILogin.LoginPresenter{

    private Context context;

    @Inject
    public Presenter(Context context) {
        this.context = context;
    }

    @Override
    public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
        Intent intent = new Intent(context,MainActivity.class);///?
        Bundle bundle = new Bundle();///?
        bundle.putString("USER", user.getUserName());
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

I have nothing in module :

@Module
public class LoginModule {
}

My questions:

  1. Is it true that I am creating new object (Intent and Bundle) in the presenter when I am using dagger?

  2. How could I use dagger to my scenario? That's mean create new instance of intent and bundle in module class?

1

There are 1 answers

1
Benjamin On BEST ANSWER

The point of MVP pattern is to separate business logic from its view. It's a common good practice not to have any android framework related code in your presenter (here your presenter depends on Context, Intent and Bundle from the android framework).

In your case, you should not create your Intent and Bundle in your presenter since it belongs to your view (MainActivity).

Your onLogin function could look like this:

@Override
public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
    // whatever is your business logic
    view.showMainActivity(user);
}

where view is an interface implemented by your MainActivity and injected in your presenter.