Gigi Gigi - 2 months ago 16
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...

protected void onCreate(Bundle savedInstanceState) {


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



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

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


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


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(;

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



return rootView;

public void onSaveInstanceState(Bundle outState) {

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

public class TabsRestaurantsAdapter extends FragmentPagerAdapter {

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

public TabsRestaurantsAdapter(FragmentManager fm) {

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

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;

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

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

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 ?????????????????????????????????



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 ;)