android – ViewPager of Fragment does not show its content when the root Fragment is replaced again-ThrowExceptions

Exception or error:

i’m implementing the fragment screen that contains root view pager which contains child fragment and the first of child fragment has a view pager which shows images.

it is shown pretty good at first time, but when the fragment which contains root view pager is removed and replaced again (with new keyword for fragment screen), roow view pager doesn’t show child fragment.

here is the portion of my source.

    @AfterViews // this is the method of fragment screen
    protected void afterViews() {
        api(HotelDetailAPI.class).getProduct("hotels", getArguments().getInt("itemId"), new Callback<Hotel>(this) {
            public void success(Hotel _hotel, Response response) {
                hotel = _hotel;
                HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getFragmentManager());
                super.success(hotel, response);

            public void failure(RetrofitError error) {

private class HotelDetailPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragments = new ArrayList<>();

        public HotelDetailPagerAdapter(FragmentManager fm) {
            Bundle data = new Bundle();
            data.putSerializable("hotel", hotel);
            Fragment fragment1 = new HotelDetailReservationFragment_();
            Fragment fragment2 = new HotelDetailReservationFragment_();
            Fragment fragment3 = new HotelDetailReservationFragment_();

        public Fragment getItem(int position) {
            return fragments.get(position);

        public int getCount() {
            return 3;

// this is the first fragment of root view pager it's not shown again.
public class HotelDetailReservationFragment extends BaseFragment {
    private Hotel hotel;

    public void afterViews() {
        hotel = (Hotel)getArguments().getSerializable("hotel");
        FragmentTransaction ft = getBaseActivity().getFragmentManager().beginTransaction();
        Fragment fragment = new ImageSliderFragment_();
        Bundle data = new Bundle();
        data.putStringArray("images", hotel.getImages());
        ft.add(, fragment);

note that fragment screen is popped from back stack when the user presses back button, and if the user is clicked item again, new instance of fragment screen is replaced
what’s wrong with them?

How to solve:

Instead of :

HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getFragmentManager());

Try :

HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getChildFragmentManager());


Instead of :

FragmentTransaction ft = getBaseActivity().getFragmentManager().beginTransaction();

Try :

FragmentTransaction ft = getChildFragmentManager().beginTransaction();


The problem is that you have nested fragments and use getFragmentManager();
You should use getChildFragmentManager() instead.

See this answer


I have solved my problem from myself.

Fundamentally, it’s not good design using fragment like a activity.
Even if fragment screen is removed and destroyed, the children fragments of fragment screen are not destroyed.

In my problem, children of the first fragment screen was not destroyed, so my pager adapter reuses children again. this causes wrong behavior of view pager.

there is various solution for this problem.

you can destroy all children fragment of fragment screen when the fragment screen is being destroyed.

or just change fragment screen to Activity.

i have applied last method, it works well 😀

have a good time 😀 thank you for up voting my question.

Leave a Reply

Your email address will not be published. Required fields are marked *