I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.
Related
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’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.
I am writing an android app that will have a number of different screens that I would like to swipe between, each screen will be a full page except for action bar header. On each screen there is the ability to open up another screen which will also be multiple screens that I would like swipeable. What is the best way to handle this. Do I have one fragment manager that holds all the screens and handle the onPageScrollStateChanged to only allow swipes between the current accessable screens or would I be best off nesting the fragments. I hope the above makes sense.
Thanks in advance
Sounds like you want to use a ViewPager to to swipe between views (Fragment extends View)
You could either:
In a single activity use a FragmentManager that switches between the parent and child Fragments, each with their own ViewPager and nested Fragments
Start a new activity to hold each ViewPager
Both are valid, if the Fragments need to communicate with each other or the Activity option one might suit the project needs better.
For the swiping between views you indeed need a ViewPager
For the nested fragments I would use a wrapper. I struggled a lot with fragments and found that this is the best way. A wrapper is very simple. This is just a fragment that holds other fragments. In the onCreate() of this fragment you get the childFragmentManager and add the fragment you originionally wanted to add. If you want to go to a new fragment you simply get the childFragmentManager again and replace the current item. This way you have nested fragment. You can add this to the backstack in order to get back navigation, but you need to override onBackPressed() inside your activity and call the method popBackStack() from the fragmentManager in order to get the first fragment back.
If you have any questions, comment below.
In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.