Search code examples
androidandroid-fragmentsandroid-viewpagerfragmentmanager

findFragmentById for a viewpager, of a tab, returns the same fragment regardless of the fragment displayed


I have a tab with two fragments. Regardless of the tab that is selected,

getSupportFragmentManager().findFragmentById(R.id.container)

returns the id of the fragment in the second tab every time.

I experimented and added another tab, repeating one of the fragments. Strange behaviour. Clicking the button on the first fragment changes the second, then clicking on the third changes the second and clicking the second changes the third. But then swiping between all the tabs resets only the first and third fragment.

Any idea how to fix or work around this?

Activity:

public class MainActivity extends AppCompatActivity {

    //The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the sections.

    private SectionsPagerAdapter mSectionsPagerAdapter;
    public android.support.v4.app.Fragment test;

    //The {@link ViewPager} that will host the section contents.

    private ViewPager mViewPager;
    private ActivityMainBinding binding;

    public interface Interface {
        public void InterfaceMethod(int number);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        //Test button1 click behaviour
        binding.button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Interface interfaceUse = (Interface)(getSupportFragmentManager().findFragmentById(R.id.container));
                interfaceUse.InterfaceMethod(1);
            }
        });
    }

    protected void buttonClick(int a, int b){
        int position = binding.tabs.getSelectedTabPosition();

    }

    //A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages.

    public class SectionsPagerAdapter extends FragmentPagerAdapter {


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

        @Override
        public android.support.v4.app.Fragment getItem(int index)
        {
            switch (index)
            {
                case 0:
                    return new UnitFragment();
                case 1:
                    return new RmFragment();
                case 2:
                    return new UnitFragment();
            }

            return null;
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Unit Converter";
                case 1:
                    return "One Rep Max";
                case 2:
                    return "Dum";
            }
            return null;
        }
    }
}

UnitFragment:

public class UnitFragment extends Fragment implements MainActivity.Interface{

    private TextView textview1;

    public void InterfaceMethod(int position) {
        textview1.setText(Integer.toString(position));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View rootView = inflater.inflate(R.layout.unit_fragment, container, false);

        //r.id
        textview1 = (TextView) rootView.findViewById(R.id.textView);

        return rootView;
    }
}

Fix:

Interface interfaceUse =(Interface) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.container + ":" + mViewPager.getCurrentItem());

Solution

  • Try using this code in your button click,

    binding.button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Interface interfaceUse = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.container + ":" + mViewPager.getCurrentItem());
           interfaceUse.InterfaceMethod(1);
        }
    });