title: One Interface for Multiple Fragments
body: <p>Can someone please tell me if I'm solving this correctly or if I should go another route?</p>
  
  <p>This is a simplified example: I have 1 Activity and 2 Fragments. Each Fragment has a button that when clicked, relays the click back to the Activity and a Toast pops up within the Activity. </p>
  
  <p>I know that a Fragment communicates with an Activity through an interface. But what If I have multiple Fragments that have a similar Interface. For example, here both Fragments use an onClick type of interface to communicate back to the Activity</p>
  
  <pre><code> static interface OnClickedListener{
      public void buttonClicked(View v);
  }
  </code></pre>
  
  <p>Is it better to</p>
  
  <p>A) Create a separate Interface class and attach it within both Fragments. For example Fragment 1:</p>
  
  <pre><code>public class Fragment1 extends Fragment implements OnClickedListener{
  
  
  private OnClickedListener clickedInterface;
  
  public Fragment1() {
      // Required empty public constructor
  }
  
  @Override
  public void buttonClicked(View v) {
  }
  
  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      this.clickedInterface = (OnClickedListener)activity;
  }}
  </code></pre>
  
  <p>Fragment 2:</p>
  
  <pre><code>public class Fragment2 extends Fragment implements OnClickedListener{
  
  private OnClickedListener clickedInterface;
  
  public Fragment2() {
      // Required empty public constructor
  }
  
  @Override
  public void buttonClicked(View v) {
  }
  
  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      this.clickedInterface = (OnClickedListener)activity;
  }
  </code></pre>
  
  <p>OR</p>
  
  <p>B) Create individual Interfaces unique to the specific Fragment and implement those in the MainActivity instead of the one Interface like mentioned above. Thank you.</p>