title: Cross-fragment communication
body: <p>I am working on an app that has an "artist list", a list of a good chunk of music bands, performers and/or composers. They are sorted by genre and relative popularity.<br>
  From the main activity you go to a "band list" activity that has two tabs in it. One of them is a "genres" tab that has a list of the general music genres and when you click on each genre you get sent to the respective activity that also has two tabs in it. The first one is the "All" tabs and the other one's the "Popular" tab. Each of the tabs holds a fragment that contains a filtered sqlite database (by "filtered" I mean I have one big database that gets filtered to show what I want in different fragments rather than using a new database for each fragment/genre). So now I have 7 genres and 14 classes; 2 ("all" and "popular") for each genre.<br>
  What I would like to do now is reduce the number of classes to only two (all/popular) or, if possible, even one, by recycling the same fragment for all lists via filtering the database again. In order to do this I need to send a string from the "genres" fragment, containing the position of the pressed list item (genre) to the activity which contains the "All" and "Popular" tabs (fragments), which will then send that string further, to the fragments, which will use it to filter the sqlite database.</p>
  
  <p>I need to add that I have, of course, read the Android Developers guide on fragment communication, but I do not know how to use their approach in my code, since I have created the fragments a little differently, including not defining them in the xml, so because of that I don't know how to use their <code>getSupportFragmentManager().findFragmentById(R.id.article_fragment);</code> method and some more things like that.  </p>
  
  <p>Here is the TabListener via which I am creating all fragments:  </p>
  
  <pre><code>public class TabListener&lt;T extends Fragment&gt; implements ActionBar.TabListener {
      private Fragment mFragment;
      private final Activity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
  
      public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
      }
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          if (mFragment == null) {
              mFragment = Fragment.instantiate(mActivity, mClass.getName());
              ft.add(android.R.id.content, mFragment, mTag);
          } else {
              ft.attach(mFragment);
          }
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          if (mFragment != null) {
              ft.detach(mFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
      }
  }
  </code></pre>
  
  <p>Thanks,<br>
  Aleksa</p>