廢話不多說,看源碼:
/**
* Return the {@link FragmentActivity} this fragment is currently associated with.
* May return {@code null} if the fragment is associated with a {@link Context}
* instead.
*
* @see #requireActivity()
*/
@Nullable
final public FragmentActivity getActivity() {
return mHost == null ? null : (FragmentActivity) mHost.getActivity();
}
大概意思爲:返回一個和此fragment綁定的FragmentActivity或者其子類的實例。相反,如果此fragment綁定的是一個context的話,可能會返回null。