Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentPagerAdapter inside Fragment

I'm having a bit of trouble implementing a design based around multiple ViewPagers.

At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.

Fragment 1 is a basic fragment with some TextViews and ImageViews. Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments. Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)

In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.

At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.

Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.

The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.

I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.

For reference My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.

I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.

like image 835
brockoli Avatar asked Aug 21 '12 22:08

brockoli


People also ask

What happened to the fragmentpageadapter?

The FragmentPageAdapter was then moved from the Activity to a separat Fragment (which now is child of that activity) as part of the refactoring. This change did not go well. The cached tabs are for instance disappearing after an orientation change.

What is fragmentpageradapter in Kotlin?

Kotlin | Java abstract class FragmentPagerAdapter : PagerAdapter Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

What is the use of fragmentmanager in Android?

A FragmentManager manages Fragments in Android, specifically, it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments. getPageTitle (int pos): (optional) Similar to getItem () this methods returns the title of the page at index pos.

What happens to a page fragment when it is not visible?

The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state.


1 Answers

This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.

If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.

like image 153
blinkmacalahan Avatar answered Sep 20 '22 11:09

blinkmacalahan