Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FragmentStateAdapter is not calling createfragment function when fragment changes

I have a slight issue with the createFragment method in FragmentStateAdapter. I would expect that createFragment is called every time when fragment changes in ViewPager2. However, the createFragment is called only when the first fragment change occurs. Then the createFragment method is never called again.

My code:

 @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position)
        {
            case 0:
            {
                myTextView.setText("0");
                return discover_page;
            }
            case 1:
            {
                myTextView.setText("1");
                return top_page;
            }
            case 2:
            {
                myTextView.setText("2");
                return friends_page;
            }
            default: return null;
        }
    }

In this code, the myTextView is set to "2" when the first fragment change occurs and never becomes anything else, even when fragments are being changed. Does anybody know why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because fragments are not (re)created all the time. They’re created and then reused. In order to have your textview change text appropriatelly, you need to use OnPageChangeCallback

Something like this:

viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);

            //your code
            switch (position)
            {
                case 0:
                {
                    myTextView.setText("0");
                    //return discover_page;
                }
                case 1:
                {
                    myTextView.setText("1");
                    //return top_page;
                }
                case 2:
                {
                    myTextView.setText("2");
                    //return friends_page;
                }
                //default: return null;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading