2

I have already searched the entire Internet, but could not solve my problem. There is a fragment with an adapter:

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}

Okey, there is good, my current page= 2 saved. Then i have:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
    CURRENT_TAB = savedInstanceState.getInt("current_tab");
    viewPager.post(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(CURRENT_TAB);
        }
    });
}

......

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  Bundle savedInstanceState) {
 super.onSaveInstanceState(savedInstanceState);
 view = inflater.inflate(R.layout.fragment_orderlist, container, false);
 savedInstanceState.getString("current_tab"));

In both situations my savedInstanceState is null. What is wrong?

AlexS
  • 918
  • 1
  • 12
  • 28
  • refer this link https://stackoverflow.com/questions/18075853/viewpager-fragments-disappear-when-change-screen-rotation?answertab=votes#tab-top –  Nov 22 '18 at 05:23
  • @AndroidTeam, how FragmentManager allows save my bundle object?... – AlexS Nov 22 '18 at 05:26
  • set into argument –  Nov 22 '18 at 05:32
  • refer this link https://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack?answertab=votes#tab-top –  Nov 22 '18 at 05:33
  • why did you write **(Dublicate)** in title ? – Manohar Nov 22 '18 at 06:14
  • @Redman because i try more then 10 answers with same problem. – AlexS Nov 22 '18 at 06:14

1 Answers1

0
   // First extend your class with Fragment like

    public class Abc extends Fragment {

    private View rootView;
    private static CustomViewPager mPager;
    private static int currentPage = 0;

    // Then Required an empty public constructor

    public Abc() {

        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
            //Now initialize the viewpager and do the magic

     mPager = rootView.findViewById(R.id.pager);
    mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



            mPager.setPagingEnabled(false);
            int NUM_PAGES = ArrayList.size();

            return rootView;