Dynamic display using Fragments and FragmentTabHost? - android

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.

Related

How to save fragments states to avoid re-creating them

I've implemented something like the below picture:
I have some Tabs and every tab may have some buttons which load some different fragments. In this situation, in Tab 1, Button 1 loads Fragment 1, and Button 2 loads Fragment 2 and each time the Fragments will be re-created. but I want Buttons to load Fragments just once and avoid re-creating next times.
Button 1 current onClick:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.commit();
How can I save fragments states to avoid reloading every time?
EDIT
The Tab 1 and Tab 2 Fragments are inside a ViewPager so I could not use ViewPager for Fragment 1 and Fragment 2.
Use below code
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.addToBackStack("Frag1");
transaction.commit();
instead of
FragmentTransaction transaction =
getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.commit();
Problem:
You are trying to replace fragment while adding them via FragmentTransaction. If you need to keep instance of them you need to use transaction.add method.
Caution:
Using this method can overlap fragments. So you need to add backgroundcolor to both fragments and make the parent layout as clickable so that you can interact
you can create your tabs with viewpager
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setOffscreenPageLimit(tabsNumber - 1); // e.g. send 2 as parameter for 3 tabs
and save your tab state

Show a nested fragment inside a tabLayout with view pager

I have main activity with 4 tabs in the settings tab I need to implement this flow:
The user click on a recyclerView item (this has been handled).
A new fragment is shown instead of the settings tab.
I tried to make a replace transaction on view pager view, but when the user click on another tab the newly created fragment is still shown and it's not replaced by view pager.
Edit: My code for showing the nested fragment:
clickListenr = settingsRecyclerAdapter.observeClickListener()
.subscribe(id -> {
if(id == 3){
getFragmentManager().beginTransaction()
.replace(R.id.view_pager,new WebViewFragment())
.addToBackStack(null)
.commit();
}
});
I suggest you to use only Framelayout in your main fragment. Then Creat two fragment named like MainFragmentTop and , SettingFragment(nested fragment). Then in your main fragment onviewcreated method relapce your MainFragmentTop fragment into framelayout. Then when you want to open setting fragment replace or add same into framelayout. So, it's become easier to use.
This is working fine.

How do I retain the state of my ViewPager in my TabLayout when I'm replacing a Fragment on top of it?

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

Fragment Transaction inside a fragment with a Tabbed actionbar not working

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.

Viewpager inside fragment and back button

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()

Categories

Resources