title: Android TabLayout Fragment with MVP
body: <p>I'm implementing an MVP app in which the Views are fragments loaded in Activities. Each Activity had 1 fragment to display. I have to change my implementation and add the TabLayout which will now display the fragments. I've tried different ways of passing the fragment to the adapter but all makes my app crash and I can't understand the error. My last try, I'm passing an arraylist of fragments(1 for now) to the adapter. At the base, I'm following <a href="https://github.com/googlesamples/android-architecture/tree/todo-mvp/" rel="nofollow noreferrer">google samples MVP todo app</a>, but I need to implement this tab layout. Please, this is for my major project, I looked everywhere and this is my last resort.</p>
  
  <pre><code>public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home);
  
          Toolbar mHomeToolbar = (Toolbar) findViewById(R.id.toolbar);    // Set to the corresponding Toolbar in the UI.
          setSupportActionBar(mHomeToolbar);
  
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    // Set to the corresponding Drawer Layout in the UI.
  
          ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mHomeToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          mDrawerLayout.addDrawerListener(mToggle);   // Set mToggle as Drawer's toggle button and listen to actions.
          mToggle.syncState();
  
          NavigationView mDrawerNavigationView = (NavigationView) findViewById(R.id.nav_view);    // Set the corresponding Navigation View in the UI.
  
          mDrawerNavigationView.setNavigationItemSelectedListener(this);  // Add listener on Navigation's items.
  
          HomeFragment homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentById(R.id.Quests_Frame); // Set to corresponding Fragment View in the UI.
  
          if (homeFragment == null) {
              homeFragment = HomeFragment.newInstance();
              FragmentLoader.loadFragmentInActivity(getSupportFragmentManager(), homeFragment, R.id.Quests_Frame);    // Display fragment in Activity.
          }
  
          repo = QuestsRepository.getInstance(QuestsDataSource.getINSTANCE());
          mHomePresenter = new HomePresenter(repo , homeFragment);
  
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
          ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
          TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
          adapter.addFragment(homeFragment);
          viewPager.setAdapter(adapter);
  
      }
  </code></pre>
  
  <p>The adapter class:</p>
  
  <pre><code>public class TabPagerAdapter extends FragmentPagerAdapter {
  
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final int tabCount = 3;
  
      public TabPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  mFragmentList.get(position);
              default:
                  return null;
          }
      }
  
      public void addFragment(Fragment fragment) {
          mFragmentList.add(fragment);
      }
  
      @Override
      public int getCount() {
          return tabCount;
      }
  }
  </code></pre>