Why ViewPager defined in activity


#1

public class CrimePagerActivity extends FragmentActivity { ViewPager mViewPager;

why ViewPager defined in activity? shouldn’t it better to define in a fragment and use just a container layout in the activity?


#2

You’d need to use child fragments (which the book does not cover) to accomplish that correctly:

public class ViewPagerFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...

        FragmentManager fm = getChildFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
            ...
        });
        ...
    }
}