1- "... is it truly safe to call requireActivity() ...?" No, as you can see from the implementation of the method:
/**
* Return the {@link FragmentActivity} this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with an activity or if associated
* only with a context.
* @see #getActivity()
*/
@NonNull
public final FragmentActivity requireActivity() {
FragmentActivity activity = getActivity();
if (activity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to an activity.");
}
return activity;
}
In case the activity is null, this method will throw an exception and if you had not handled this exception as probably you might not have handled nullity of the getActivity() the app would crash. Compiler does not show a warning simply because there is a null check in this method.
2- "But how can a fragment not be associated with an activity? I mean, don't I always need an activity to show anything in Android?"
True. An attached fragment needs an activity, but, simply because a getActivity() method is called does not mean its fragment is in attached state. So, it might happen in listeners or some callbacks, that one might call getActivity() while activity is destroyed eg. due to a rotation. Because of such situation you should check the nullity of returned value by getActivity(). When the activity is destroyed, the Associated context is not the activity's context. There are several interesting insights into this matter and good suggestions that you can read about them here: getActivity() returns null in Fragment.