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();
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.
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());
Im working with fragments on my main activity I have a Edit Text and buttons in my main activity But when I opens fragment page and click exact at the spot of Edit Text the keyboard activates.
So it means the below activity is also in working at same time
Fragments are designed to be inside an activity, if you don't want to make your activity visible you can use a full screen layout for fragment, to remove the clicks you can replace .add command with .replace when transacting to fragments give a background color to the root view of fragment layout also. I think this would help
YourFragment fragment = new YourFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.ll_job_view_fragment_container, fragment)
.addToBackStack(TAG_FRAGMENT_YOUR_VIEW)
.commit();
Edit
The replace may not help, If you inflate different fragments in the same container you just give the attribute android:clickable="true" to the top most parent view of each fragment that will help.
my problem is put fragment component on two fragments in the same layout. It is a dialog fragment which covers the window with menu(1 fragment) and table(2 fragment). How can I do that ?
You could use relative layout to put fragment on the top of other fragments in the same layout.
http://developer.android.com/guide/topics/ui/layout/relative.html
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