title: Cannot setMaxLifecycle for Fragment not attached to FragmentManager: ViewPager
body: <p>I have tried with so many given examples but nothing worked for me. When I am trying to remove a page dynamically from viewPager then I am getting exception:</p>
  
  <p><strong>Cannot setMaxLifecycle for Fragment not attached to FragmentManager</strong></p>
  
  <p>My PagerAdapter is given below:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyPagerAdapter extends FragmentStatePagerAdapter {
  
      private final Context mContext;
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
      long baseId = 0;
  
      public MyPagerAdapter(Context context, FragmentManager fm) {
          super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
          mContext = context;
      }
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFragment(Fragment fragment, String title, int position) {
          mFragmentList.add(position, fragment);
          mFragmentTitleList.add(position, title);
      }
  
      public void removeFragment(int position) {
          mFragmentList.remove(position);
          mFragmentTitleList.remove(position);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  </code></pre>
  
  <p>And to remove page I am calling method like</p>
  
  <pre><code>myPagerAdapter.removeFragment(viewPager.getCurrentItem());
  </code></pre>
  
  <p>I have tried with below given method too, but still getting exception</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public int getItemPosition (Object object) {
      int index = mFragmentList.indexOf (object);
      if (index == -1)
          return POSITION_NONE;
      else
          return index;
  }
  </code></pre>