title: Passing a presenter to a Fragment in Android (without a constructor)
body: <p>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 ;)</p>
  
  <p>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?</p>
  
  <p>I would <strong>like</strong> 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?</p>
  
  <p>If so, how else can I pass in the presenter?</p>
  
  <p>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.</p>
  
  <h2>My 2 interfaces:</h2>
  
  <pre><code>public interface IClickableListPresenter {
  
      ListAdapter createListAdapter();
      void onListItemClick(int position);
  
  }
  
  public interface ITabbable {
  
      String getTitle();
      Fragment getFragment();
  
  }
  </code></pre>
  
  <h2>2 example presenters:</h2>
  
  <pre><code>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
      }
  
  }
  </code></pre>
  
  <h2>My fragment:</h2>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <h2>And finally, the class that instantiates the fragments:</h2>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter{
  
      private ITabbable tabs[] = {
              new ClickableListFragment("Artist", new ArtistPresenter()),
              new ClickableListFragment("Title", new TitlePresenter()),
              //...
      };
  
      //...
   }
  </code></pre>