title: Is it preferable to use Activity.onAttachFragment or Fragment.onAttach to communicate between an Activity and a nested fragment?
body: <p>The Android documentation suggests that to communicate from an activity to a hosted fragment, the fragment can define a callback interface and require that the host activity implement it. The basic pattern involves implementing <code>onAttach</code> in your fragment, and casting the activity to a callback inteface. See <a href="http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity">http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity</a>.</p>
  
  <p>Here's an example of providing a fragment some initialization data, as well as listening for a navigation callback.</p>
  
  <pre><code>public class HostActivity extends Activity implements FragmentHost {
    @Override
    UiModel getUiModel() {
      return mUiModel;
    }
    @Override
    FragmentNavListener getNavListener() {
      return mNavListener;
    }
  ...
  }
  
  public class HostedFragment extends Fragment {
    @Override
    public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof FragmentHost) {
        FragmentHost host = (FragmentHost) activity;
        setUiModel(host.getUiModel());
        setNavListener(host.getFragmentNavListener());
      }
    }
    ...
  }
  </code></pre>
  
  <p>Compare this to using <code>onAttachFragment</code> in the host activity to explicitly initialize the fragment:</p>
  
  <pre><code>public class HostActivity extends Activity {
    @Override
    public void onAttachFragment(Fragment fragment) {
      super.onAttachFragment(fragment);
      if (fragment instanceof HostedFragment) {
        HostedFragment hostedFragment = ((HostFragment) fragment);
        hostedFragment.setUiModel(mUiModel);
        hostedFragment.setNavListener(mNavListener);
      }
    }
    ...
  }
  </code></pre>
  
  <p>To me, it seems like the first pattern has some drawbacks:</p>
  
  <ol>
  <li>It makes the fragment harder to use from different activities, since
  since all of those activities must implement the required interface. I can imagine cases where a given fragment instance doesn't require being fully configured by the host activity, yet all potential host activities would need to implement the host interface.</li>
  <li>It makes the code slightly harder to follow for someone unfamiliar with the pattern being used. Initializing the fragment in onFragmentAttached seems easier to  follow, since the initialization code lives in the same class that creates the fragment.</li>
  <li>Unit testing using a library like Robolectric becomes harder, since when calling onAttach, you must now implement FragmentHost rather than just calling onAttach(new Activity().</li>
  </ol>
  
  <p>For those of you who've done activity to fragment communication, what pattern do you find preferable, and why? Are there drawbacks to using <code>onAttachFragment</code> from the host activity?</p>