废话不多说,看源码:
/**
* 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。