0

I am currently working on an application with ActionBar tabs inside one of its fragments (main navigation is NavigationDrawer). The fragment's onCreate():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    actionBar = activity.getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Resources r = getResources();
    //(…) tabNames initialization

    adapter = new CustomTabAdapter(getFragmentManager());
}

and onCreateView():

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment, container, false);
    viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(this);

    for (String tabName : tabNames) {
        actionBar.addTab(actionBar.newTab()
                .setText(tabName)
                .setTabListener(this));
    }
    return rootView;
}

There are 4 tabs, each of them with its own Fragment and layout. CustomTabAdapter returns new instance of proper fragment in its getItem(int). There are 4 tabs.

And now my problem is:

  1. I start the application.
  2. I choose this tab fragment from NavigationDrawer list. Everything is working just fine.
  3. I choose another fragment from NavigationDrawer list which means that tab fragment is replaced with it.
  4. I choose tab fragment again. And fragment related to tab that was selected before choosing another fragment from NavigationDrawer list, and one's adjacent to it are not recreated (blank screen under ActionBar tabs). I checked and onCreateView(…) methods of those fragment are not called. So after changing device orientation, or choosing tab not adjacent and this one again proper layout is shown.

How can I make it work as it should (showing proper layout on reentering tab fragment from NavigationDrawer list instead of blank space)? I run out of ideas.

Tiero
  • 107
  • 11

2 Answers2

1

Finally, I found a solution. My CustomTabAdapter was extension of FragmentPagerAdapter. I changed it to be extension of FragmentStatePageAdapter, and now fragments are recreated.

More details in this answer by @Louth.

Community
  • 1
  • 1
Tiero
  • 107
  • 11
0

It takes fragments from cache without recreation

Yury Finchenko
  • 1,035
  • 13
  • 19