java – When you rotate the screen, the Fragment changes to the previous one

Question:

There is one activity, in it the fragments change in the container as follows:

public void switchFragment(Fragment fragment, String tag) {
    if (fragment != null) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment, tag)
                .addToBackStack(tag)
                .commit();
    }
}

When the device is rotated, all fragments are recreated normally, except for one, inside which the ViewPager (although perhaps the problem is not in it). When rotated, this fragment is simply closed and the previous one is displayed. Going to debug did not reveal the problem. The OnCreateView() method of the given fragment is called and completely passes. Further, its code:

public class WordTranslateFragment extends Fragment {

private WordTranslateController controller;

public WordTranslateFragment() {
    controller = WordTranslateController.getInstance();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_translate_training, container, false);
    setHasOptionsMenu(true);

    final ViewPager mPager = (ViewPager) rootView.findViewById(R.id.pager);
    controller.generateTest();
    final WordTranslateSlideAdapter mPagerAdapter = new WordTranslateSlideAdapter(getActivity().getSupportFragmentManager(), controller.getSize() + 1);
    mPager.setAdapter(mPagerAdapter);

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        int currentPage = 0;

        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }

        @Override
        public void onPageSelected(int i) {
            if(i == controller.getSize())
                mPagerAdapter.notifyDataSetChanged();
            if(currentPage <= i && controller.swapToPage(i)) {
                currentPage = i;
            } else {
                mPager.setCurrentItem(currentPage);
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }
    });
    return rootView;
}

Also here is the PageAdapter code

    public class WordTranslateSlideAdapter extends FragmentStatePagerAdapter {

    private int count = 0;

    public WordTranslateSlideAdapter(FragmentManager fm, int size) {
        super(fm);
        count = size;
    }

    @Override
    public Fragment getItem(int i) {
        return WordTranslateSlideFragment.getNewInstance(i);
    }

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

And a fragment of pages:

public class WordTranslateSlideFragment extends Fragment {

private static final String EXTRA_ID = "id";
private WordTranslateController.BlockWords block;
ArrayList<Button> buttons = new ArrayList<Button>();
private int idSlide;

public static WordTranslateSlideFragment getNewInstance(int id) {
    WordTranslateSlideFragment f = new WordTranslateSlideFragment();
    Bundle bdl = new Bundle(1);
    bdl.putInt(EXTRA_ID, id);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.word_translate_adapter, container, false);

    idSlide = getArguments().getInt(EXTRA_ID);
    block = WordTranslateController.getInstance().getBlock(idSlide);

    final TextView aTextView = (TextView) rootView.findViewById(R.id.aTextView);
    TextView qTextView = (TextView) rootView.findViewById(R.id.qTextView);

    qTextView.setText(block.rightWord.getName());
    aTextView.setText(block.rightWord.getTranslate());
    aTextView.setVisibility(View.INVISIBLE);

    buttons.add((Button) rootView.findViewById(R.id.button));
    buttons.add((Button) rootView.findViewById(R.id.button2));
    buttons.add((Button) rootView.findViewById(R.id.button3));
    buttons.add((Button) rootView.findViewById(R.id.button4));
    View.OnClickListener buttonListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button click = (Button) v;
            aTextView.setVisibility(View.VISIBLE);
            if(click.getText().toString().equalsIgnoreCase(block.rightWord.getTranslate())) {
                click.setBackgroundColor(getResources().getColor(R.color.green));
                blockButtons();
                WordTranslateController.getInstance().setResult(idSlide, true);
            } else {
                click.setBackgroundColor(getResources().getColor(R.color.red));
                blockButtons();
                WordTranslateController.getInstance().setResult(idSlide, false);
            }
        }
    };
    for (int i=0; i < buttons.size() && i < block.answers.size(); i++) {
        buttons.get(i).setText(block.answers.get(i).getTranslate());
        buttons.get(i).setOnClickListener(buttonListener);
    }
    Random random = new Random();
    int z = Math.abs(random.nextInt()) % 4;
    buttons.get(z).setText(block.rightWord.getTranslate());

    return rootView;
}

Answer:

Perhaps, passing to the constructor of the ViewPager adapter not the FragmentManager -a of the activity, but the FragmentManager -a of the fragment will help you:

final WordTranslateSlideAdapter mPagerAdapter;
mPagerAdapter = new WordTranslateSlideAdapter(getChildFragmentManager(), controller.getSize() + 1);
Scroll to Top