Gigi Gigi - 3 months ago 27
Android Question

Fragments nested do not appear after screen rotation

I'm going craziy :)

I have this situation

Activity -> RestaurantsFragment -> [viewPager / FragmentPageAdapter] Two Fragments

I want to retain all the fragments after the screen rotation but what happens is that


  • the Activity is correctly destroyed and recreated

  • the RestaurantsFragment is correctly retained and reattached to the activity

  • the two fragments do not appear. Their methods onAttach, onCreateView etc...are not invoked



Here is my code...

Activity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

....

if (savedInstanceState== null) {
fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMNET_RESTAURANTS);
if(fragment==null) {
fragment = new RestaurantsFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container, fragment, (TAG_FRAGMNET_RESTAURANTS);

}

....
}


RestaurantsFragment.java

private TabsRestaurantsAdapter tabsRestaurantsAdapter;
private ViewPager viewPager;
private int position;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Luigi-RFragment-" + id, "onCreate");

setRetainInstance(true);


Log.d("Luigi-RFragment-" + id, "savedInstanceState==null");
tabsRestaurantsAdapter = new TabsRestaurantsAdapter(getChildFragmentManager());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Luigi-RFragment-" + id, "onCreateView");

View rootView = inflater.inflate(R.layout.ristoranti_fragment, container, false);

viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(tabsRestaurantsAdapter);

position = 0;
if (savedInstanceState != null) {
position = savedInstanceState.getInt("position");
}

viewPager.setCurrentItem(position);

setHasOptionsMenu(true);

return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putInt("position", viewPager.getCurrentItem());
}


TabsRestaurantsAdapter.java

public class TabsRestaurantsAdapter extends FragmentPagerAdapter {

private String titles[] = new String[]{"I TUOI PREFERITI", "TUTTI"};
private MenusFavTabFragment menusFavTabFragment;
private RestaurantsAllTabFragment restaurantsAllTabFragment;

public TabsRestaurantsAdapter(FragmentManager fm) {
super(fm);

menusFavTabFragment = new MenusFavTabFragment();
restaurantsAllTabFragment = new RestaurantsAllTabFragment();
}

@Override
public Fragment getItem(int index) {
Log.d("Luigi-adapter", "getItem" + index);
switch (index) {
case 0:
// Favourite Fragment
return menusFavTabFragment;

case 1:
// All Restaurant
return restaurantsAllTabFragment;

}

return null;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment f = (Fragment) super.instantiateItem(container, position);
return f;
}

public CharSequence getPageTitle(int position) {
return titles[position];
}

@Override
public int getCount() {
// get item count - equal to number of tabs
return titles.length;
}

}


Consider that when the RestaurantsFragment is reattached, the adapter (old) is setted again in the viewPager and the method of the adapter instantiateItem is invoked

The two fragments nested have nothing special

( I have also tried to recreate a new adapter each time (managing thre reuse of the old fragments) and it works but only for one rotation...but why should i recreate the adapter ? )

Why they do not appear ?????????????????????????????????

Thanks

Answer

This a bug in the support library v4.

For more info Nested fragments not being retained in support library.

Personally, I do the nasty thing of setting the configuration changes on my manifest to orientation and screenSize. I didn't give myself a time to think of a better way ;)