不需要FragmentManager
直接从中获取,因为在被调用Activity
中提供了替换方法:Fragment
getParentFragmentManager
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
从已弃用的文档中getFragmentManager
:
* @deprecated This has been removed in favor of <code>getParentFragmentManager()</code> which
* throws an {@link IllegalStateException} if the FragmentManager is null. Check if
* {@link #isAdded()} returns <code>false</code> to determine if the FragmentManager is
* <code>null</code>.
替换方法的文档getParentFragmentManager
:
* Return the FragmentManager for interacting with fragments associated
* with this fragment's activity. Note that this will available slightly
* before {@link #getActivity()}, during the time from when the fragment is
* placed in a {@link FragmentTransaction} until it is committed and
* attached to its activity.
*
* <p>If this Fragment is a child of another Fragment, the FragmentManager
* returned here will be the parent's {@link #getChildFragmentManager()}.
*
* @throws IllegalStateException if not associated with a transaction or host.
只需注意检查isAdded
以确保FragmentManager
不为空。