I have a Fragment which contains a ListView say FragmentA, when I add another Fragment which contains another ListView say FragmentB:
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, FragmentB)
.commit()
I noticed that getView() of ListView in FragmentA is still getting called even though the top fragment is now FragmentB, any reason why and how do I prevent this behavior?
You're stacking fragments on top of each other.
Instead of add use replace to replace any existing fragments in that container.
Related
As the title suggest, is it a good idea to perform FragmentTransaction inside a fragment?
The way I view this is that, FragmentTransaction requires a containerView id and the Fragment to inflate when calling FragmentTransaction.replace(). The containerView where to get the ID usually resides in the activity layout. So it seems like a good idea to do FragmentTransaction inside the activity rather than in the Fragment.
But I got confused when using SharedElementTransition for fragments. Now it requires a list of Views which will be the sharedElements. These views reside in the Fragment. Now it seems logical to do the FragmentTransaction inside the Fragment itself.
Can anyone guide me on how to compensate for both scenarios?
There's nothing wrong on doing FragmentTransaction inside a fragment as long as it is not redundant, like for example:
"Container" Fragment
"Child Fragment" that holds view pager
View pager has fragments
Here, you can remove the child fragment and hold the view pager on container fragment instead.
For FragmentTransaction inside a fragment, you can actually swap the fragment itself by calling getActivity().getSupportFragmentManager(). Here, transaction is happening on Activity not on the fragment. But if you intended to use FragmentTransaction to swap the child of the parent fragment, you can use getChildFragmentManager() on the fragment.
Also, child fragments is actually supported by android.
I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.
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 am working on an application that contains three tabs and each tab has a fragment associated with it. On certain conditions i have to display child tab within second parent tab. But if that condition is not true then child tabs should not load and the parent tab should load one of the fragment i used in child tabs. For example, i have three fragments FragmentA, FragmentB, and FragmentC. Now if the condition is true then i will display tabs and load FragmentB and FragmentC inside FragmentA using Fragment tab host. But is condition is false i want to display FragmentB in FragmentA.
I tried using Fragment.replace() but it fails.
I think this is not a completely new issue.
You can find an answer here:
Separate Back Stack for each tab in Android using Fragments
Regards
Francesco.
In my intention you should override the onHiddenchange() for fragment A and check the condition over here.
I was able to solve the issue using a common fragment. In FragmentA's onCreateView if condition is true create Tabs using FragmentTabHost and add FragmentB and FragmentC to the tab. If condition is false then inflate a view using
View fragmentView = Inflator.Inflate(R.id.fagment_layout, FragmentB);
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentView, fragmentB);
transaction.commit();
It worked for me.
I have a FragmentActivity that contains a FrameLayout. I use the following code to add Fragments to the Fragment Activity.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment, fargmentTag);
ft.addToBackStack(null);
ft.commit();
Now R.id.fragment_content is FrameLayout. This adds a Fragment onto the view. But the underlying view still remains visible. Meaning the one lying view is seen through the new fragment ? What am i doing wrong.
Kind Regards
for the next fragment use ft.replace(); so the previous one will be repalced.
Either ft.remove() the old fragment, or use ft.replace() instead of ft.add().
ft.replace() should work fine, otherwise you have to update your question for a better understanding.
Using ft.replace will replace any previously added fragment with this fragment but if the activity has some view added to its layout rather than a fragment ,then the fragment added through add or replace will show its content over it rather than replacing it as it is not a fragment.
either add another layout within your main layout and assign it some id and then add the fragment to that container.
Hope it helps.