0

I have a fragment that implements a ViewPager, in this fragment, I save the ViewPager status using the onSaveInstanceState() method. I can save it in the bundle using viewPager.getCurrentItem(), but when I go to restore the status of the ViewPager, the viewPager.setCurrentItem() doesn't seem to work. How can I save the fragment status correctly?

This is my fragment code

public class HomeFragment extends Fragment {
private ViewPager viewPager;
private ViewPagerAdapter adapter;
Integer[] colors=null;
List<Model> models;
private String[] mDescriptions;
private String[] mTitle;
private String mPlayerName;


@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("currentItem", viewPager.getCurrentItem());
}




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

    models = new ArrayList<>();
    Resources res = getResources();
    mDescriptions = res.getStringArray(R.array.newGameDescription);
    mTitle = res.getStringArray(R.array.newGame);
    models.add(new Model(R.drawable.ic_launcher_background,mTitle[0], mDescriptions[0]));
    models.add(new Model(R.drawable.ic_launcher_background,mTitle[1], mDescriptions[1]));
    models.add(new Model(R.drawable.ic_launcher_background,mTitle[2], mDescriptions[2]));
    models.add(new Model(R.drawable.ic_launcher_background,mTitle[3], mDescriptions[3]));

    adapter = new ViewPagerAdapter(getContext(), models, mPlayerName);
    int orientation = getResources().getConfiguration().orientation;
    adapter.setOrientation(orientation);

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    viewPager = (ViewPager) view.findViewById(R.id.gameModeViewPager);
    viewPager.setAdapter(adapter);
    viewPager.setPadding(130,0,130,0);

    return view;
}


@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state for checked position.
        viewPager.postDelayed(new Runnable() {

            @Override
            public void run() {
                int item = savedInstanceState.getInt("currentItem");
                viewPager.setCurrentItem(item);
            }
        }, 100);
    }
}

}

Any suggestion and review are welcome, thanks in advance

JayJona
  • 469
  • 1
  • 16
  • 41

1 Answers1

0

onCreateView does not return bundle value from onSaveInstanceState you have to get it from onActivityCreated

     @public void onActivityCreated(Bundle savedInstanceState) {
                  super.onActivityCreated(savedInstanceState);
                      if (savedInstanceState != null) {
                // Restore last state for checked position.
    viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                {
                   int item = savedInstanceState.getInt("currentItem");
                   viewPager.setCurrentItem(item);    
        }
     }); 
   }
Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
  • also in this case it doesn't seem to work, making a print of item inside onActivityCreated () correctly prints the number of the page saved in the bundle, but the viewPager.setCurrentItem (item); set page number 0. – JayJona Jun 14 '19 at 12:56
  • again, unfortunately the loaded page is always 0 :( – JayJona Jun 14 '19 at 13:04
  • please update your question with edited code so we can understand the problem – Mohammad Sommakia Jun 14 '19 at 13:06
  • again, it seems not to work. At this point I wonder how to test the thing right. I simply change the page and I switch to a page other than 0, now I rotate the phone (with active rotation) so that the layout is reloaded and at this point I should see in the viewPager the page I had saved in the bundle but it is not. – JayJona Jun 14 '19 at 13:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194986/discussion-between-james-and-mohammad-sommakia). – JayJona Jun 15 '19 at 11:24