So I have a problem that I can't seem to solve. I have a ViewPager in one of my activities, say MainActivity. I am implementing all the required methods to save and retrieve instance state when the activity gets killed in the background. But when the activity tries to restore its state, the fragments are restored but they are not attached to the viewpager so all I get is a white screen.
Here is the relavant code:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//..
if (savedInstanceState==null) {
setupViewPager();
}
}
private void setupViewPager() {
mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mAdapter.addFrag(FirstFragment.newInstance(mFeedItem), "INFO");
//Note: mFeedItem is obtained from the intent and I have verified
// that it is properly restored.
mAdapter.addFrag(SecondFragment.newInstance(mFeedItem), "SERVICES");
mViewPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
FirstFragment.java
public static FirstFragment newInstance(Workshop item) {
FirstFragment f = new FirstFragment();
f.mItem = item;
return f;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("feed_item",Workshop.packToGson(mItem));
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_item_detail_info, container, false);
if (savedInstanceState!=null) {
mItem = Workshop.extractGson(savedInstanceState.getString("feed_item"));
}
return v;
}
So if I navigate to another activity and then the MainActivity
gets killed in the background ( I am using the Don’t Keep Activities setting to test this), this happens:
1) The Activity gets restored properly, no issues there.
2) The onCreateView
of the fragments are called, with the savedInstanceState
intact.
3) But, the ViewPager and Adapter in my Activity are null
. The fragments dont seem to get attached back to the adapter/viewpager.
So what am I missing here? Am I supposed to do anything to properly restore the fragments?
PS:
I am able to solve the issue quickly by replacing this in my MainActivity
:
if (savedInstanceState==null) {
setupViewPager();
}
with just setupViewPager();
, so that a new adapter and fragments are created irrespective of whether it is restoring or launching for the first time. But that comes with the drawback of resetting the state of the fragments. Also, when I do that, I find that there are two instances of each fragment, one which is restored and a new one, with only the new instances actually displayed. So I dont believe that it is the best approach.