Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:23:09+00:00 2026-06-14T08:23:09+00:00

I have a ViewPager with three Fragments , each one shows a List (or

  • 0

I have a ViewPager with three Fragments, each one shows a List (or Grid).

In the new Android API level 17 (Jelly Bean 4.2), one of the features is Nested Fragments. The new functionality description says:

if you use ViewPager to create fragments that swipe left and right and
consume a majority of the screen space, you can now insert fragments
into each fragment page.

So, if I understand right, now I can create a ViewPager with Fragments (with a button inside for example) inside, and when user press the button show another Fragment without loose the ViewPager using this new feature.

I’ve expended my morning trying to implement this several different ways, but I can´t get it working… Can somebody add a simple example of how to implement this?

PS: I’m only interested in doing at this way, with getChildFragmentManager to learn how works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:23:10+00:00Added an answer on June 14, 2026 at 8:23 am

    Assuming you have created the correct xml layouts. It is now very simple to display fragments in a ViewPager that is hosted by another Fragment.

    The following is a parent fragment that displays child fragments:

    class ParentViewPagerFragment : Fragment() {
    
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root = inflater.inflate(R.layout.fragment_parent_viewpager, container, false)
    
        val viewPager = root.findViewById(R.id.viewPager) as ViewPager
        // Important: Must use the child FragmentManager or you will see side effects.
        viewPager.adapter = MyAdapter(childFragmentManager)
    
        val tabStrip = root.findViewById<TabLayout>(R.id.pagerTabStrip)
        tabStrip.setupWithViewPager(viewPager)
    
        return root
      }
    
      class MyAdapter internal constructor(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    
        override fun getCount(): Int = 4
    
        override fun getItem(position: Int): Fragment {
          val args = Bundle().apply { putInt(ChildFragment.POSITION_KEY, position) }
          return ChildFragment.newInstance(args)
        }
    
        override fun getPageTitle(position: Int): CharSequence = "Tab $position"
      }
    
      companion object {
        val TAG: String = ParentViewPagerFragment::class.java.name
      }
    }
    

    It is important to use Fragment.getChildFragmentManager() when instantiating the FragmentPagerAdapter. Also note that you cannot use Fragment.setRetainInstance() on the children fragments or you’ll get an exception. The imports were omitted for brevity.

    Source code can be found at: https://github.com/marcoRS/nested-fragments

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewPager with multiple fragments. In one Fragment I play audio. When
I have a FragmentActivity using a ViewPager to serve several fragments. Each is a
I am using the Android Compatibility Package with API Level 10. I have a
I have a ViewPager with 3 Fragments in there. Each Fragment uses a AsyncTaskLoader
I have a ViewPager within a Fragment which contains three Fragments we'll call V1,
I have a ViewPager hooked up to a FragmentPagerAdapter that's displaying three fragments. The
I have created an android app that uses a ViewPager to swipe through three
I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new
I have a viewpager including 3 pages. I set one viewgroup in each page
I have a ViewPager with a dynamic number of pages in it. Each of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.