My fragment (one of the parent fragments) has a ViewPager, and that ViewPager will load a number of child fragments by using FragmentPagerAdapter.
I observed the child fragments will show properly first time after application run, and after that the child fragments will show blank after I move to other parent fragment and come back to that parent fragment. One of the child fragment will redraw properly if I swipe to the last child fragment.
In the parent fragment onCreateView, use getChildFragmentManager() when create FragmentPagerAdapter.
Instead of using
XXXAdapter xxxAdapter = new XXXAdapter(getActivity().getSupportFragmentManager());
Use
XXXAdapter xxxAdapter = new XXXAdapter(getChildFragmentManager());
Related
I am having an activity with a viewPager which holds two fragments. For example, let's name the two fragments as Fragment A and Fragment B. Each of the fragments are having a set of cards. If suppose, I select a set of cards from fragment A and swipe to fragment B, the selected cards still remain selected when I swipe back to fragment A. How do I deselect them on swiping to the fragment B?
Use replace() method while doing fragment transaction. It will remove all the previous fragment from the containerView and add the new one.
I have a parent fragment containing TabLayout and ViewPager.
The TabLayout has 3 tabs (fragments) managed by FragmentStatePagerAdapter.
After the user clicks on a button on the 3 fragment, I would like to replace it with different fragment, without reloading the parent fragment.
I tried to remove the fragment and add the new one and call notifyDataSetChanged() and the fragment is indeed replaced, but the TabLayout custom view headers are gone...
Any idea how can I do it right?
Thanks.
You can have the 3rd Fragment working as a holder for two child Fragments which you can switch based on your app's logic (clicking the button you mention).
The key for this alternative is that the 3rd Fragment in the ViewPager will be the parent of the two Fragments that will be switching.
The two child Fragments will communicate with the parent Fragment for the parent to take care of doing the switching.
For the switching the parent will use a ChildFragmentManager instead of the FragmentManager used for Fragments that are managed by Activities.
I have a FrameLayout container in which I use the replace method to put a fragment when an item is selected from the navigation drawer.
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, fragment.getFragmentTag()).commit();
I have a fragment which has a ViewPager within it. I have 3 fragments which are displayed in a tab layout with the ViewPager. When I select another item from the navigation drawer the fragment with the ViewPager is replaced but the fragments within are not detached. The system calls only the fragment with the ViewPager onDetach/onDestroy etc. methods but not the methods of the fragments it contains.
I am using FragmentPagerAdapter and I tried with the FragmentStatePager adapter. Nothing changes. I am using the default setOffscreenPageLimit(1);
How can I get my ViewPager fragments methods onDetach/onDestroy called?
Thanks!
Are you using the FragmentManager or the child Fragment Manager? If you create a Fragment inside another Fragment and you want the inner Fragment to behave like the outer, you should get the child fragment manager from the outer fragment and use it to create the inner fragments. See this.
I start out with a fragment with a framelayout that I replace for a child fragment. This chlid fragment has a listview, when I click the list view I want to replace this fragment with yet another child fragment.
From what I have read I should do this by replacing the framelayout from the first parent fragmen, but this framelayout is not reachable from the child fragment.
What is the right thing to do here?
You don't need to pass the actual View instance, just the resource id of the container:
getFragmentManager().beginTransaction().replace(R.id.content, new ChildFragment()).commit();
or a better solution design-wise is to notify your parent Fragment about a selection in the ListView and ask it to replace Fragments:
getChildFragmentManager().beginTransaction().replace(R.id.content, new ChildFragment()).commit();
I am using FragmentTabHost to display the Tab inside the fragment. From parent fragment I am calling a child fragment inside the child fragment I want to hide the tab please any one help me on this