title: Fragment activity communication by passing Context object to onAttach()
body: <p>Im trying to implement fragment to activity communication.</p>
  
  <p>Went through <a href="http://developer.android.com/training/basics/fragments/communicating.html" rel="nofollow noreferrer">android developer</a> doc where an Activity object is passed to onAttach life cycle and set up the Fragment-Activity communication.</p>
  
  <p>This <a href="http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)" rel="nofollow noreferrer">documentation</a> asks to pass Context object instead of Activity. I replaced all the Activity objects by Context objects in the life cycle method onAttach. But it is throwing a NullPointerException while calling the method of the interface from Fragment. </p>
  
  <pre><code>@Override
  public void onAttach(Context context) {
      super.onAttach(context);
      try {
          colourChangerInterface = (ColourChangerInterface) context;
      }
      catch (Exception exp){
          System.out.println("error!");
      }
  }
  </code></pre>
  
  <p>Can anyone please give a small example of the usage in the new way ?
  Thanks</p>
  
  <p><strong>Edit</strong> :</p>
  
  <p>Found <a href="https://stackoverflow.com/questions/32077086/android-onattachcontext-not-called-for-api-23">this</a> link where detail discussion is there on the same issue.
  The issue is because of the broken API 'onAttach()'; it doesn't get called at all when Context object is passed. </p>
  
  <p>A simple and quick solution found from the above link is to move the code from onAttach to onCreate.</p>