Frage

I would like to try and use the MVP pattern for a simple Android application I am writing. This is my first time using the MVP pattern, as I'm still learning, so please be gentle ;)

I have a fragment that I would like to use in conjunction with 4 different presenters. The problem I have is, how can I pass a different presenter to each instance?

I would like to pass the presenter to a constructor, however when Android recreates the fragment, it will call the default constructor. Does this mean it will no longer hold a reference to the presenter?

If so, how else can I pass in the presenter?

I've included some psuedo code of what I'd like to do below. Please note I've just typed this straight into the browser so there may be some silly mistakes, but hopefully you get a rough idea of what I mean.

My 2 interfaces:

public interface IClickableListPresenter {

    ListAdapter createListAdapter();
    void onListItemClick(int position);

}

public interface ITabbable {

    String getTitle();
    Fragment getFragment();

}

2 example presenters:

public class ArtistPresenter implements IClickableListPresenter {

    public ListAdapter createListAdapter(){
        // Create a ListAdapter containing a list of artists
    }

    public void onListItemClick(int position){
        // Handle the click event
    }
}

public class TitlePresenter implements IClickableListPresenter {

    public ListAdapter createListAdapter(){
        // Create a ListAdapter containing a list of song titles
    }

    public void onListItemClick(int position){
        // Handle the click event in a completely different way 
        // to the ArtistPresenter
    }

}

My fragment:

public class ClickableListFragment extends ListFragment 
                                    implements ITabbable {

    private IClickableListPresenter presenter;
    private String title;

    // What can I do instead of this constructor?
    public ClickableListFragment(
            String title, IClickableListPresenter presenter){

        this.title = title;
        this.presenter = presenter;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setListAdapter(presenter.createListAdapter());
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        presenter.onListItemClick(position);
    }

    public Fragment getFragment(){
        return this;
    }

    public String getTitle(){
        return title;
    }

}

And finally, the class that instantiates the fragments:

public class TabsPagerAdapter extends FragmentPagerAdapter{

    private ITabbable tabs[] = {
            new ClickableListFragment("Artist", new ArtistPresenter()),
            new ClickableListFragment("Title", new TitlePresenter()),
            //...
    };

    //...
 }
War es hilfreich?

Lösung

You could do something like this

public class TabsPagerAdapter extends FragmentPagerAdapter{

    private ITabbable tabs[] = {
        ClickableListFragment.newInstance(ClickableListFragment.Type.ARTIST),
        ClickableListFragment.newInstance(ClickableListFragment.Type.TITLE),
        //...
    };
}

In ClickableListFragment

public enum Type {
    ARTIST,

    TITLE
}

public static MyFragment newInstance(final Type fragmentType) {
    final MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putSerializable(TYPE, fragmentType);
    fragment.setArguments(args);
    return fragment;
}

and in onCreate

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Type tempType = (Type) getArguments().getSerializable(TYPE);
    if(tempType == Type.ARTIST){
        variable = new ArtistPresenter(); 
        .... what you want with that variable
    .
    .
    .
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top