There is an activity. Here is all that it does:

fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.main_fragment_container, ViewPagerFragment.newInstance(hero)).commit(); 

That is, adds a fragment with the ViewPager to the container:

 public class ViewPagerFragment extends Fragment { private ViewPager mViewPager; private TabLayout mTabLayout; private Toolbar mToolbar; private Hero hero; private int currentPage; public static ViewPagerFragment newInstance(Hero hero) { Bundle args = new Bundle(); args.putParcelable("hero", hero); ViewPagerFragment fragment = new ViewPagerFragment(); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (getArguments()!= null) hero = getArguments().getParcelable("hero"); if (savedInstanceState == null) { currentPage = 0; } else { currentPage = savedInstanceState.getInt("Page"); } return inflater.inflate(R.layout.fragment_view_pager, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { mToolbar = (Toolbar) view.findViewById(R.id.toolbar_pager_fragment); ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar); mViewPager = (ViewPager) view.findViewById(R.id.main_view_pager); mTabLayout = (TabLayout) view.findViewById(R.id.main_tabs); mViewPager.setAdapter(new MainPagerAdapter(getActivity(), getChildFragmentManager(), hero)); mTabLayout.setupWithViewPager(mViewPager); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { currentPage = position; Log.i(TAG, "onPageScrolled: "+ position); } @Override public void onPageSelected(int position) { currentPage = position; Log.i(TAG, "onPageSelected: "+ position); } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("Page",currentPage); Log.i(TAG, "onSaveInstanceState " ); } } 

But for some reason, savedINstanceState always null . The remaining questions on this topic on stack'e did not help.

  • If I'm wrong, correct it. I need an empty public constructor - Flippy
  • @ SergeyGrushin unfortunately not, 1) I don’t have any designers, so by default, there is implicitly an empty 2) I tried and it doesn’t help. - newakkoff

1 answer 1

Fragments are automatically restored in the activation when it is re-created after turning. Apparently, every time you re-create, add a new fragment on top of the existing one. And in the new fragment, of course, there is no saved state. Try to check the activation of the fragment in the activation and, if it already exists, do not add a new one Something like this (add a tag when adding a fragment so that you can search by it later):

 Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); if(fragment == null) { fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.main_fragment_container, ViewPagerFragment.newInstance(hero), "TAG").commit(); } 

It is also worth noting that the FragmentStatePagerAdapter automatically saves the currently selected page when you rotate the screen and restores and applies it yourself. No further action is required. In any case, if the ViewPager is in actviti, this is true

  • one
    FragmentStatePagerAdapter does not save, apparently because it is in the fragment, but the check helped, and the information is new, I will know, thank you very much - newakkoff