title: How to pass selected item from ListFragment to previous Fragment after calling popBackStack()?
body: <p>I have prepared a very simple test case with 1 activity and 2 fragments for my question:</p>
  
  <ul>
  <li><a href="https://github.com/afarber/android-newbie/blob/q15/MyList/src/de/afarber/mylist/MainActivity.java" rel="nofollow noreferrer">MainActivity.java</a> (stores selected item in shared preferences)</li>
  <li><a href="https://github.com/afarber/android-newbie/blob/q15/MyList/src/de/afarber/mylist/MyMainFragment.java" rel="nofollow noreferrer">MyMainFragment.java</a> (displays a text view and a "select" button)</li>
  <li><a href="https://github.com/afarber/android-newbie/blob/q15/MyList/src/de/afarber/mylist/MyListFragment.java" rel="nofollow noreferrer">MyListFragment.java</a> (displays a list of items)</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/8qeLm.png" alt="screenshot"></p>
  
  <p>Initially MainActivity displays MyMainFragment.</p>
  
  <p>When user touches "Select item..." button, MainActivity displays MyListFragment with:</p>
  
  <pre><code>public void selectedButtonClicked() {
      SharedPreferences prefs = getSharedPreferences(PREFS, 0);
      int index = prefs.getInt(INDEX, -1);
      Fragment fragment = MyListFragment.newInstance(index);
      getFragmentManager().beginTransaction()
          .addToBackStack(null)
          .replace(R.id.root, fragment, LIST)
          .commit();
  }
  </code></pre>
  
  <p>After user touches one of the items in the list, MainActivity stores the selected item position in shared preferences and calls <code>popBackStack()</code> to display the MyMainFragment again:</p>
  
  <pre><code>public void itemSelected(int index) {
      SharedPreferences prefs = getSharedPreferences(PREFS, 0);
      Editor editor = prefs.edit();
      editor.putInt(INDEX, index);
      editor.commit();
      getFragmentManager().popBackStack();
      // How to show the index in mSelectedTextView?
  }
  </code></pre>
  
  <p>I.e. the MainActivity in my app does all the "heavy work": it displays 1 of 2 fragments and keeps the user data in shared preferences.</p>
  
  <p>My question is: how to display the selected item (the "Item 07" in the above screenshot) in the <code>mSelectedTextView</code> of the MyMainFragment?</p>
  
  <p>I'm looking for a way to do it properly, without any hacks (for example fragments shouldn't touch shared preferences).</p>
  
  <p><strong>UPDATE:</strong> I've tried the suggestion by corsair992 (thanks!)</p>
  
  <pre><code>public void itemSelected(int index) {
      SharedPreferences prefs = getSharedPreferences(PREFS, 0);
      Editor editor = prefs.edit();
      editor.putInt(INDEX, index);
      editor.commit();
      getFragmentManager().popBackStackImmediate();
      MyMainFragment f = (MyMainFragment) getFragmentManager().findFragmentById(R.id.root);
      // call a method in MyMainFragment to update mSelectedTextView
      f.setText("Selected index: " + index);
  }
  </code></pre>
  
  <p>but it only works, when user touches an item in MyListFragment. It does not however work, when user returns to MyMainFragment by touching Back button.</p>