1

I have done lot of search on Google and also on Stackoverflow but still I'm confused, so asking a question here.

I have this small MVP design pattern -

SigninView.java

public interface SigninView{

    void onSuccess();
    void onError();
    void onComplete();
}

SigninPresenter.java

public interface SigninPresenter {

    void signIn(String emailID, String password);
}

SigninModel.java

public class SigninModel implements SigninPresenter {

    private SigninView mSigninView;

    public SigninModel(SigninView mSigninView) {
        this.mSigninView = mSigninView;
    }

    @Override
    public void signIn(String emailID, String password) {

        if(emailID.equals("abc@example.com") && password.equals("123")){
            mSigninView.onSuccess();
        }
        else{
            mSigninView.onError();
        }

        mSigninView.onComplete();

    }
}

I want to implement the SigninView on a Fragment and define the SigninPresenter there itself like this -

SigninPresenter mSigninPresenter = new SigninModel(view_of_mvp);
mSigninPresenter.signIn("adadada", "asads");

See one reference here. I want to implement a View and define a Presenter like this but on a Fragment - https://github.com/ashokslsk/Comprehensive-MVP/blob/master/app/src/main/java/com/ashokslsk/mvpexample/MainActivity.java

How to achieve that ?

Gissipi_453
  • 1,250
  • 1
  • 25
  • 61

2 Answers2

2

You don't actually need to pass the context, but rather the implementation of your SigninView. So you need to make your fragment implement SigninView

MyFragment implements SigninView

and simply initialize the presenter with this, instead of context. In fact, you presenter shouldn't know much about the Android SDK, so it shouldn't deal with contexts. See this answer.

SigninPresenter mSigninPresenter = new SigninModel(this);

EDIT:

You had the activity like this:

public class MainActivity extends AppCompatActivity implements SigninView 

All you have to do is make your fragment implement SigninView:

public class MyFragment extends Fragment implements SigninView 

And then, in onCreateView you can initialize the presenter like this:

signinPresenter = new SigninPresenterImpl(this);
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • Another thing. Do developers don't use Fragments in MVP generally like I'm doing ? I'm asking this question because I searched a lot but I didn't actually find something like this. I like to keep things simple. – Gissipi_453 Apr 09 '18 at 12:15
  • 1
    We actually do, it really depends on your needs. The point is the View in MVP can be an Activity, a Fragment or even a View object. If you take a look an this code sample by Google https://github.com/googlesamples/android-architecture/blob/todo-mvp/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragment.java they use the fragment as a View in MVP, – Levi Moreira Apr 09 '18 at 12:17
  • 1
    So it's pretty usual to have fragments as the V in MVP, in the end it depends on your needs. – Levi Moreira Apr 09 '18 at 12:18
  • In the answer above, how can we pass 'this' as context in a fragment ? – Gissipi_453 Apr 09 '18 at 12:21
  • Fragments don't extend from Context (like activities and services) so you can't pass a fragment as a context. You can call this.getContext() to get the context from a fragment though – Levi Moreira Apr 09 '18 at 12:23
  • See I have updated my question. When defining a presenter in the Fragment, I need a pass a View of MVP to the Presenter constructor. Thats where I'm having problem. – Gissipi_453 Apr 09 '18 at 12:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168562/discussion-between-gissipi-453-and-levi-albuquerque). – Gissipi_453 Apr 09 '18 at 13:00
  • thanks. Accepted as correct answer. That solves my problem. – Gissipi_453 Apr 10 '18 at 04:17
1

Your View already implements getContext(). Just add it in your interface and call it in your SigninModel :

SigninPresenter.java

public interface SigninView {

    void onSuccess();
    void onError();
    void onComplete();

    Context getContext();
}

SigninModel.java

mSigninView.getContext();
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Thomas Mary
  • 1,535
  • 1
  • 13
  • 24