title: Callback 'magic' in master detail flow
body: <p>The Eclipse generated master detail flow has some callback magic in the class which extends <code>ListFragment</code> - I say magic because that's what it looks like when you are an Android and a Java noob, all at once :)</p>
  
  <p>Given the code below can someone answer a few questions for me:</p>
  
  <ol>
  <li>What does the <code>onAttach</code> method of the <code>ListFragment</code> do with <code>mCallbacks = (Callbacks) activity</code>?</li>
  <li>Which <code>onItemSelected</code> method is called in <code>onListItemClick</code> method of <code>ListFragment</code>, the one which needs implementation or <code>onItemSelected</code> in <code>FragmentActivity</code>?</li>
  <li>All these <code>onItemSelected</code> methods take an <code>id</code> of type <code>String</code> (because <code>DummyContent</code> <code>id</code> is a <code>String</code>). If I changed <code>DummyContent</code> <code>id</code> to a <code>long</code>, which <code>onItemSelected</code> methods would I need to change? I tried changing the one in <code>FragmentActivity</code> but this has <code>@Override</code> so I wasn't allowed to :(</li>
  </ol>
  
  <p>Thank you</p>
  
  <pre><code>public class RecordingListFragment extends ListFragment {
  
      private static final String STATE_ACTIVATED_POSITION = "activated_position";
  
      private Callbacks mCallbacks = sDummyCallbacks;
      private int mActivatedPosition = ListView.INVALID_POSITION;
  
      public interface Callbacks {
  
          public void onItemSelected(String id);
      }
  
      private static Callbacks sDummyCallbacks = new Callbacks() {
          @Override
          // NEEDS IMPLEMENTATION - i guess????
          public void onItemSelected(String id) {
          }
      };
  
  ...
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          if (!(activity instanceof Callbacks)) {
              throw new IllegalStateException("Activity must implement fragment's callbacks.");
          }
  
          mCallbacks = (Callbacks) activity;
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          mCallbacks = sDummyCallbacks;
      }
  
      @Override
      public void onListItemClick(ListView listView, View view, int position, long id) {
          super.onListItemClick(listView, view, position, id);
          mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
      }
  
  ...
  
  }
  </code></pre>
  
  <p>And one more file...</p>
  
  <pre><code>public class RecordingListActivity extends FragmentActivity
          implements RecordingListFragment.Callbacks {
  
      private boolean mTwoPane;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_recording_list);
          getActionBar().setDisplayHomeAsUpEnabled(true);
  
          if (findViewById(R.id.recording_detail_container) != null) {
              mTwoPane = true;
              ((RecordingListFragment) getSupportFragmentManager()
                      .findFragmentById(R.id.recording_list))
                      .setActivateOnItemClick(true);
          }
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case android.R.id.home:
                  NavUtils.navigateUpFromSameTask(this);
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onItemSelected(String id) {
          if (mTwoPane) {
              Bundle arguments = new Bundle();
              arguments.putString(RecordingDetailFragment.ARG_ITEM_ID, id);
              RecordingDetailFragment fragment = new RecordingDetailFragment();
              fragment.setArguments(arguments);
              getSupportFragmentManager().beginTransaction()
                      .replace(R.id.recording_detail_container, fragment)
                      .commit();
  
          } else {
              Intent detailIntent = new Intent(this, RecordingDetailActivity.class);
              detailIntent.putExtra(RecordingDetailFragment.ARG_ITEM_ID, id);
              startActivity(detailIntent);
          }
      }
  }
  </code></pre>