I know it sounds like a duplicate of FragmentStatePagerAdapter IllegalStateException:
The FragmentStatePagerAdapter
is a horrible piece of code riddled with bugs acknowledge or not by Google and so I use this code to fix this particular crash:
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Yet another bug in FragmentStatePagerAdapter that destroyItem is called on fragment that hasnt been added. Need to catch
try {
super.destroyItem(container, position, object);
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
Use the following
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(f, key);
instead of
mFragmentManager.putFragment(state, key, f);
and pass the bundle explicitly..
For reference,
http://developer.android.com/reference/android/app/FragmentTransaction.html#add%28android.app.Fragment,%20java.lang.String%29
You could try using mFragmentManager.add();
Write in Activity onCreate() method :
pager = (ViewPager) findViewById(R.id.pager);
adapter = new SwipePagerAdapter(getSupportFragmentManager());
pageOneFragment = new PageOneFragment();
adapter.addFragment(pageOneFragment);
Adapter code:
public class SwipePagerAdapter extends FragmentStatePagerAdapter
{
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
public SwipePagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
return mFragments.get(position);
}
@Override
public int getCount()
{
return mFragments.size();
}
public void addFragment(Fragment fragment)
{
mFragments.add(fragment);
notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
}
@Override
public CharSequence getPageTitle(int position)
{
return super.getPageTitle(position);
}}
Double-check whether you've implemented onSaveInstanceState
and onRestoreInstanceState
in your activity, and verify that they're correctly saving and loading the state of your fragments.
Try using
use fragmentTransaction.add()