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.
Related
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 single activity that uses a bunch of different Fragments. I have a TabContainer Fragment that holds a TabLayout which uses a ViewPager to handle tab navigation. Each Tab is its own Fragment.
In one of my tabs, I want to tap and place a fragment on top of my Tabbed fragment. This is meant to be a "details" sort of screen, so I don't want the tabs to be visible. I'm using this and it works as intended:
fragmentTransaction.replace(android.R.id.content, fragmentToDisplay).addToBackStack(null).commit();
Now, when I navigate back, the content in my tab is empty. The content in the tab directly next to that tab is also empty. When I navigate two tabs away, the content is recreated and the normal functionality returns. Why is content not being recreated on the tabs initially when I remove my "detail" fragment?
It turns out that I was simply not passing the correct FragmentManager to the FragmentStatePagerAdapter.
I needed to call getChildFragmentManager() on the Fragment, not getSupportFragmentManager() on the activity.
Thanks to these two posts for the answer:
Fragment in ViewPager not restored after popBackStack
and: Replacing ViewPager with Fragment - Then Navigating Back
I asked this question before getChildFragmentManager() and viewpager without any responses, probably because the explanation was too long.
Simply put I have a viewpager inside a fragment. The pages inside that viewpager need to be replaced through a series of screens. Pressing back on those screens will not exit the application. How do I do this?
challenges:
viewpager xml does not use framelayout. when I use the replace method, what container
id do I use?
when replacing the fragments why would I use getChildFragmentManager each time if that is private to the current fragment?
#goonedroid: that's not the case I'm looking for here. The only similarity is that the fragment has a viewpager. My viewpager's pages need to be replaced when clicked:
I have a navdrawer. Clicking item 2 shows FragmentA. Clicking FragmentA replaces it with FragmentB. Clicking FragmentB replaces it with FragmentC. Clicking FragmentC takes you back to FragmentB. Fragments are added to the backstack for proper back navigation.
Clicking item 1 in the navdrawer shows FragmentZ with a viewpager. The viewpagers pages are just FragmentA and all the aforementioned behavior. But going from A to B here, then pressing back shows no pages in the viewpager.
I finally figured out a solution, not sure why this works because I thought an Activity's getSupportFragmentManager() returns the same fragment manager as its fragment's getFragmentManager().
Anyway, instead of getFragmentManager() in the fragments, use getActivity().getFragmentManager()
I have a Fragment with list view. OnItem click of that list view I will replace the fragment's container with the new fragment. The new fragment will have the view pager and fragment state pager adapter. This adapter will create three fragments once. Now, all the five fragments (List fragment, fragment which has the view pager and the three fragments which are created by the fragment state pager adapter) are in the fragment manager. If I came back to the fragment which has list view. All the fragments except the one with list view should be removed from the fragment manager. But, they are not removing.
The problem is, If I click an item in the list view It will create three fragments all the time. And If I do orientation change, the fragment manager is recreating all the fragments. Each time I do orientation change three fragments are getting added to the fragment manager.
Is there any way to remove the fragment associated with view pager when I come back from the view pager fragment.
Without seeing the code I think this is your problem:
Every time your orientation changes, you add a new fragment manager transaction to the backstack. When you pop the backstack, you don't go back to the initial state but to another state where you still have the same fragment visible.
There are multiple ways around it. Choose one:
Don't add the fragment manager transaction to the backstack when you replace the fragments. When you need to go back to the initial fragment, just replace the contents with the fragment that you want. You can always keep a reference to the fragment in the activity if you don't want to re-create the fragment.
Pop the backstack before replacing the fragments and adding another transaction to the backstack. This way you are not accumulating transactions in the backstack. When you pop, the backstack you will always come to the previous state.
Name your fragment transaction so you can pop the backstack with popBackstack("fragmentTransactionName", 0) or popBackStack(transactionId, 0)
See http://developer.android.com/reference/android/app/FragmentManager.html
Hope this helps
I'm initializing three fragments in my FragmentActivity that are actually tabs (TabHost and ViewPager), among those one is a list fragment.
Next I have another fragment activity with three tabs(fragments), Now what I want is on click of the list item of the list fragment, to call another fragment of 2nd fragment activity (which means that now the tabs that should come up are from another fragment activity ), also I want to pass some value from the selected list item to that next fragment. How can I achieve this?