title: How to show a text from one fragment in another fragment?
body: <p><strong>I WAS WORKING BY THE EXAMPLE YOU JUST GAVE ME AS DUPLICATE, READ THE ENTIRE POST, TY</strong></p>
  
  <p>I'm making a simple app with sliding menu by using the template provided by android studio. And I have to use fragments to switch between items of the sliding menu.</p>
  
  <p>What I wanna do? (check app design at the bottom)</p>
  
  <p>When I click on button pass data it should set the text in the Fragment B to the text that I entered in a Fragment A but without going directly to the Fragment B. So when I press the button pass data then I wanna go to the sliding menu and select item that containts fragment B and then I wanna see the text from Fragment A. Thanks in advance.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements FragmentA.DataPassListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      drawer.setDrawerListener(toggle);
      toggle.syncState();
  
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
          @Override
          public boolean onNavigationItemSelected(MenuItem item) {
              int id = item.getItemId();
  
              if (id == R.id.nav_camera) {
                  // Handle the camera action
  
                  displayFragmentA();
              } else if (id == R.id.nav_gallery) {
  
                  displayFragmentB();
  
              } else if (id == R.id.nav_slideshow) {
  
              } else if (id == R.id.nav_manage) {
  
              } else if (id == R.id.nav_share) {
  
              } else if (id == R.id.nav_send) {
  
              }
  
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
              drawer.closeDrawer(GravityCompat.START);
              return true;
          }
      });
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  
  @Override
  public void passData(String data) {
  
      FragmentB fragmentB = new FragmentB();
      Bundle args = new Bundle();
      args.putString("data", data);
      fragmentB.setArguments(args);
      //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentB).commit();
      //it works with the commented part but I dont wanna go directly to the fragment when I press pass data button
  
  
  }
  
  public void displayFragmentA() {
  
      FragmentA frag = new FragmentA();
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.fragment_container, frag);
      fragmentTransaction.commit();
  
  
  }
  
  public void displayFragmentB() {
  
      FragmentB frag = new FragmentB();
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.fragment_container, frag);
      fragmentTransaction.commit();
  
  
  }
  }
  </code></pre>
  
  <p>Fragment A code:</p>
  
  <pre><code>public class FragmentA extends Fragment {
  
  DataPassListener mCallback;
  
  public interface DataPassListener {
      public void passData(String data);
  }
  
  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      // Make sure that container activity implement the callback interface
      try {
          mCallback = (DataPassListener) activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString()
                  + " must implement DataPassListener");
      }
  }
  
  
  public FragmentA() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_a, container, false);
  
      final EditText input = (EditText) view.findViewById(R.id.etInputID);
  
      Button passDataButton = (Button) view.findViewById(R.id.bPassDataID);
      passDataButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
  
              mCallback.passData(input.getText().toString());
  
          }
      });
  
  
      return view;
  }
  
  }
  </code></pre>
  
  <p>Fragment B code:</p>
  
  <pre><code>public class FragmentB extends Fragment {
  
  TextView showReceivedData;
  
  
  public FragmentB() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_fragment_b, container, false);
  
      showReceivedData = (TextView) view.findViewById(R.id.showReceivedData);
  
      Bundle args = getArguments();
      if (args != null) {
  
           showReceivedData.setText(args.getString("data"));
  
      } else {
  
          Toast.makeText(getActivity(), "didnt get the bundle from mainactivity", Toast.LENGTH_LONG).show();
      }
  
  
      return view;
  }
  
  
  }
  </code></pre>
  
  <p>Design of the app:</p>
  
  <p><a href="https://i.stack.imgur.com/p3Udu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p3Udu.jpg" alt="Fragment A"></a></p>
  
  <p><a href="https://i.stack.imgur.com/NQhHc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQhHc.jpg" alt="Fragment B"></a></p>