I have a strange problem with one Fragment
of my ViewPager
. I have a ViewPager
with three Fragment
.
Everything was running ok till now. when app starts, the ViewPager
(with FragmentPagerAdapter
) attached the three fragments so I could navigate swiping from one to another without any problem.
the problem came when I tried to restart the app manually (after logging out) like that:
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Now when I logout and app restart application crash and through an IllegalStateException Fragment not attached to Activity
when trying to access to resources from on of the fragments.
Here is the code where I attach fragments
, the adapter
:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public BaseListFragment getItem(int index) {
switch (index) {
case 0:
case R.id.index_explore:
BaseListFragment fragment0 = new Fragment0();
fragment0.setUserVisibleHint(true);
return fragment0;
case 1:
case R.id.index_main:
if (fragment instanceof FragmentList)
((FragmentList) fragment).removeMap();
return new Fragment1();
case 2:
case R.id.index_my_stuff:
BaseListFragment fragment2 = new Fragment2();
fragment2.setUserVisibleHint(true);
return fragment2;
default:
if (fragment instanceof FragmentList)
((FragmentList) fragment).removeMap();
return new Fragment1();
}
}
@Override
public int getCount() {
return 3;
}
}
Then, I set the adpater
:
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAppSectionsPagerAdapter);
It is Fragment0 which cause the crash. I don't understand why it happens, since it should re-build everything (so the adapter with fragments too) like it does when application starts for the first time. Maybe there is something that I am assuming that is not correct? I have previously search for some similar question, and there are a lot related with this exception, but don't resolve my case, which I think is a particular one.