Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Grace Jang
Grace Jang
4,416 Points

How were we able to instantiate a new FragmentPagerAdapater if its an abstract class?

In the Android Fragments course, we ended up instantiating a FragmentPagerAdapter (see code below) but it is an abstract class - which means technically we aren't able to instantiate it. How did we do it in the course?

// how was I able to instantiate a FragmentPagerAdapter if its an abstract class viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { @Override public Fragment getItem(int position) { return position == 0 ? ingredientsFragment : directionsFragment; }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "Ingredients" : "Directions";
        }
    });

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Grace - We're really not creating an instance of FragmentPagerAdapter, but an instance of an anonymous class extending FragmentPagerAdapter.