title: Android - onAttach(Context) not called for API 23
body: <p>I've updated the SDK to the latest version (API 23) and the <code>onAttach(Activity)</code> method for fragment is deprecated. So instead of using that method, now I'm using <code>onAttach(Context)</code> but this one is not called during the lifecycle. The activity is an instance of <code>AppCompatActivity</code> from v7 and the fragment is an instance of class Fragment (<code>android.app.Fragment</code>).</p>
  
  <p>Any ideas how to get the <code>onAttach</code> working in API 23?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I found some answers that can help you to understand and fix this problem: <br><br>
  <li>As I mentioned before I’m using the activity from support v7 (AppCompatActivity) and the fragment from android.app. </li><br>
  <li>For showing fragments I get the fragment manager using the method getFragmentManager on the activity, so the fragment manager is an instance of FragmentManager from android.app -> and here is the problem</li><br>
  <li>If you run the application on a device with API 23 (I’ve run it on the AS Emulator), you’ll see that the onAttach(Context) method will be called. 
  During the process for showing a fragment by the FragmentManager, at a specific moment the onAttach(..) method is called. Using the getFragmentManager() you’ll get an instance of the fragment manager available for the Android version which is running on that device (for example API 22, 23). In case the application is running on a device with API &lt; 23 the onAttach(Context) method is not available, the fragment manager doesn’t know that in API 23 there is a method onAttach(Context), so that’s why it is not called for the API &lt; 23 -> the solution for this kind of problems is using the FragmentManager from support libraries. </li><br></p>
  
  <p>Solutions: </p>
  
  <ol>
  <li><p>Using getSupportFragmentManager() will force you to use a Fragment from the support lib. So the first solution is to replace all fragments with the fragment from support lib and using getSupportFragmentManager().</p></li>
  <li><p><em>Solution that I've already implemented is to handle 2 possibilities</em> (1. the app is running on a device with API &lt; 23, the app is running on a device with API >= 23).</p></li>
  </ol>
  
  <p>Shortly, in my implementation I have a base class for all fragments from the project and I added this code there:</p>
  
  <pre><code>/*
   * onAttach(Context) is not called on pre API 23 versions of Android and onAttach(Activity) is deprecated
   * Use onAttachToContext instead
   */
  @TargetApi(23)
  @Override
  public final void onAttach(Context context) {
      super.onAttach(context);
      onAttachToContext(context);
  }
  
  /*
   * Deprecated on API 23
   * Use onAttachToContext instead
   */
  @SuppressWarnings("deprecation")
  @Override
  public final void onAttach(Activity activity) {
      super.onAttach(activity);
      if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.M) {
          onAttachToContext(activity);
      }
  }
  
  /*
   * Called when the fragment attaches to the context
   */
  protected void onAttachToContext(Context context) {
  }
  </code></pre>
  
  <p>Now I simply override the onAttachToContext(Context) method on all fragments where I need this.   </p>