In the ViewPager install two fragments

Adapter:

public class ViewPagerAdapter extends FragmentPagerAdapter { ArrayList<Fragment> fragments = new ArrayList<>(); ArrayList<String> titles = new ArrayList<>(); public ViewPagerAdapter(FragmentManager fm) { super(fm); } public void addFragments(Fragment fragment, String title) { this.fragments.add(fragment); this.titles.add(title); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle (int position) { return titles.get(position); } } 

I install the adapter in ViewPager

 viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragments(new ListWordFragment(), "Список"); viewPagerAdapter.addFragments(new AddWordFragment(), "Добавить слово"); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); 

So, the first fragment in it is a sheet and in the second there is a form to fill in where, when you click on a button, the word that is entered in edit text should appear in the first fragment ie in the sheet. And how to make such an interaction between the two fragments?

    1 answer 1

    Fragments of your own do not exist. They exist in the framework of some kind of activity. The interaction between the fragments is organized using this Activiti. In this case, the fragment implements the interface that Activiti will use.

    • The fragment is not required to implement the interface, because activit has full knowledge of the nature of the fragment that is on it and can invoke the public methods of the fragment without violating any principles. But the fragment has the right to access the activation only by interface. - Yura Ivanov
    • @YuraIvanov Agreed. In the above example, one of the fragments receives information from the user, accordingly, Activiti should notify about this and can do this using the interface - ZigZag