Fragment inside a fragment with ViewPager - android

Is it possible to put a fragment inside a fragment in Android? Consider this: Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?

Is it possible to put a fragment inside a fragment in Android?
Using the Android Support package's backport of fragments, yes. Also, native fragments on Android 4.2 and higher (API Level 17+) support nested fragments. However, native fragments from API Level 11-16 do not.
Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
This sample project demonstrates having fragments in a ViewPager and having the ViewPager itself be in a fragment. The key is that the FragmentManager you supply to your FragmentPagerAdapter must be the child fragment manager of the outer fragment (i.e., getChildFragmentManager()).

Related

How to add a fragment and an activity in a viewpager so that the fragment appears on swipe?

I want the activity to be on the screen and a fragment to appear on swipe. Can I use viewpager? Viewpagers are generally used for multiple fragments. Can an activity and fragment be added in the viewpager?
Yes you can use viewpager, but, you cant put an activity inside a viewpager. only fragments. Its fairly easy to convert an activity to a fragment. With this you may now construct your activity in this heirarchy:
Activity
-ViewPager
--List of Fragments

Android ViewPager not calling onDetach/onDestroy/onDestroyView for when being replaced in a container

I have a FrameLayout container in which I use the replace method to put a fragment when an item is selected from the navigation drawer.
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, fragment.getFragmentTag()).commit();
I have a fragment which has a ViewPager within it. I have 3 fragments which are displayed in a tab layout with the ViewPager. When I select another item from the navigation drawer the fragment with the ViewPager is replaced but the fragments within are not detached. The system calls only the fragment with the ViewPager onDetach/onDestroy etc. methods but not the methods of the fragments it contains.
I am using FragmentPagerAdapter and I tried with the FragmentStatePager adapter. Nothing changes. I am using the default setOffscreenPageLimit(1);
How can I get my ViewPager fragments methods onDetach/onDestroy called?
Thanks!
Are you using the FragmentManager or the child Fragment Manager? If you create a Fragment inside another Fragment and you want the inner Fragment to behave like the outer, you should get the child fragment manager from the outer fragment and use it to create the inner fragments. See this.

Android - how to slide screens, inside a fragment

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.

Can i pass an Instance of a FragmentActivity into a Fragment transaction

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.

Nested Fragments - communication between child fragments in a parent fragment in a viewpager

I'm working on the initial design of an app here and I've run into some issues with how I'd like things to work.
Some background to start:
The app is using ActionBarSherlock for the overall UI. It's a tabbed design which uses a ViewPager along with a FragmentStatePagerAdapter to swap between the different tabs(which are SherlockFragments).
One tab in particular is a master-detail style SherlockFragment which contains and displays two nested fragments, a SherlockListFragment(the master) and a regular SherlockFragment(the detail).
All the examples I've seen on how to create a master/detail with fragments show the list communicating with the detail via an interface callback which is then implemented in the FragmentActivity that the master/detail fragments are attached to.
In my case though there is no FragmentActivity, the two Fragments are inside a parent SherlockFragment. How can I get the listFragments onItemSelected to update the detailFragment? Is it possible to set up an interface callback between two Fragments directly(or through the parent Fragment) without involving a FragmentActivity?

Categories

Resources