Application which I\'m writing has 3 Fragments. Main Fragment Activity has a Search Box with Search button when I press on a Search button the f
It's hard to pinpoint the exact problem but I'm almost sure it has to do with the way you built the adapter for the ViewPager.
Everything works Okay until I press HOME button wait for 30 minutes and relaunch application, after I press Search button in FragmentYellowPages Fragment application crashes
As the Activity is in the background(and apparently killed) it will recreate its content when comming back to the foreground. At this point the ViewPager will rebuild its content, but its adapter will not call getItem() as it has all the data to recreate the previous fragments on it own. So you'll end up with two fragment version, the ones currently visible in the ViewPager(so you see the onCreateView() being called) and those that are in the enum. Those fragments from the enum aren't tied to the Activity and so the entire callbacks stack isn't run for them, so onCreateView() will not be called, which will make your lvYellowPages ListView null.
Btw, this is the first time I've seen the fragments of an adapter being built in a enum and I recommend that you avoid this.
The solution is to not reference the fragments of the ViewPager outside. Make the getItem() method return a Fragment instance and if you need to access those fragment there are ways to do it without storing the ViewPager's fragment in lists/enums etc(a quick search on stackoverflow should show you a question about this):
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentFavorites();
case 1:
return new FragmentCategories();
case 2:
return new FragmentYellowPages();
}
return null;
}
@Override
public int getCount() {
return 3;
}
Also:
setRetainInstance(true);
I wouldn't use this. It doesn't make sense to retain a fragment used by a ViewPager(as I don't think you're planing to move it somewhere else) and it will also not protect you from the activity being killed in the background.