2

I am implementing a ViewPager using FragmentStatePagerAdapter. The code samples are below.

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Log.d("--GetItem", "---");

        switch (position) {

            case 0:
                return ScreenSlidePageFragment.newInstance(b1);
            case 1:
                return ScreenSlidePageFragment.newInstance(b2);
            case 2:
                return ScreenSlidePageFragment.newInstance(b3);
            default:
                return null;
        }

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.d("--InstItem", "--");
        return super.instantiateItem(container, position);

    }

    @Override
    public int getCount() {
        return NUM_PAGES; //set as 3
    }


}

ScreenSlidePageFragment is as follows.

public class ScreenSlidePageFragment extends Fragment implements View.OnClickListener{


    Bundle b;

    public ScreenSlidePageFragment() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {

        Log.d("----onCreateFrgmnt--", "11");

        super.onCreate(savedInstanceState);



    }


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

        Log.d("----onCreateViewFrgmnt-", "11");

        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_screen_slide_page, container, false);

        return rootView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

        Log.d("----onViewCreatedFrgmnt", "11");


        b = getArguments();

        TextView tv_city = (TextView) view.findViewById(R.id.city_field);
        TextView tv_update = (TextView) view.findViewById(R.id.updated_field);
        TextView tv_day = (TextView) view.findViewById(R.id.day);

        tv_day.setText("DAY " + String.valueOf(b.getInt("day")));
        tv_city.setText(b.getString("city"));

        tv_update.setText("Last update on : " + b.getString("date"));

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    public static ScreenSlidePageFragment newInstance(Bundle myBundle) {

        ScreenSlidePageFragment f = new ScreenSlidePageFragment();
        f.setArguments(myBundle);
        return f;
    }
}

In the MainActivity, I am trying to handle the orientation change. I use the following piece of code.

if (savedInstanceState != null) {

    setContentView(R.layout.activity_main);

    myViewPager = (MyCustomViewPager) findViewById(R.id.pager);


    b1 = savedInstanceState.getBundle("Fragment1");
    b2 = savedInstanceState.getBundle("Fragment2");
    b3 = savedInstanceState.getBundle("Fragment3");



    myViewPager.setOffscreenPageLimit(NUM_PAGES - 1);

    myPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());

    myViewPager.setAdapter(myPagerAdapter);

}

The fragments are recreated on orientation change. onCreateView as well as onViewCreated are being called (checked LogCat). The problem arises when I try to update the view in the fragments. For the update I am using following code:

FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter) myViewPager.getAdapter();

ScreenSlidePageFragment fragment1 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 0);
ScreenSlidePageFragment fragment2 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 1);
ScreenSlidePageFragment fragment3 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 2);

But when I call fragment1.getView(), it is returning null. However, the same code runs well when I don't change the orientation.

How can I get the fragment views on orientation change? I checked some of the solutions posted where they say to save the fragments in array, but those are not working for me. Are the codes I have put, not able to do the task? Thank you.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Sujal
  • 1,447
  • 19
  • 34

1 Answers1

2

As long as you don't update the fragments (which only happens on orientation change) your code will run fine.

However, your update code will always return null for getView() as the views haven't been created yet. instantiateItem() will create a new Fragment when called, that hasn't been added to your application yet and such all the lifecycle callbacks (including onCreateView(...) and onViewCreated(...)) haven't been called yet.

Due to the complexity of correctly updating a ViewPager's fragments I'll refer you to an excellent existing answer. You can also find more details in this question.

Community
  • 1
  • 1
TR4Android
  • 3,200
  • 16
  • 21