title: How to add onBackPressedCallback to fragment?
body: <p>I've seen this mentioned in a couple of places but cant quite work out how to implement it as there are no example online, I'm wondering whether I've misinterpreted its meaning.</p>
  
  <p><a href="https://developer.android.com/reference/androidx/activity/ComponentActivity" rel="nofollow noreferrer">the android developer link for addOnBackPressedCallback </a></p>
  
  <p>I was wondering whether this means I can add a callback inside my fragment to pick up the <code>onBackPressed()</code> before it hits the activity. </p>
  
  <p><strong>In my fragment I've implemented</strong></p>
  
  <pre><code>implements OnBackPressedCallBack
  </code></pre>
  
  <p><strong>then implemented the method</strong></p>
  
  <pre><code>@Override
  public boolean handleOnBackPressed() {
      Log.d(TAG, "handleOnBackPressed: hello");
      return true;
  }
  </code></pre>
  
  <p><strong>But its not picking it up and going straight to main activity <code>onBackPressed()</code>.</strong></p>
  
  <p>I read on further and saw that My fragment needed to extend from FragmentActivity</p>
  
  <pre><code>public class MainExampleFragment extends FragmentActivity implements View.OnClickListener, OnBackPressedCallback {
  </code></pre>
  
  <p>and instead of </p>
  
  <pre><code>appCompatActivity.getSuppportFragmentManager
  </code></pre>
  
  <p>to call</p>
  
  <pre><code>FragmentActivity.getSupportFragmentManager 
  </code></pre>
  
  <p>instead, but this throws and error in the Fragment.newInstance method when trying to set the bundle. </p>
  
  <p>Has anyone info on doing this please thanks</p>