Fragment 后台堆栈中的 ListView 仍在调用 getView()

ListView in Fragment backstack still calling getView()

我有一个 Fragment 其中包含 ListViewFragmentA,当我添加另一个包含另一个 ListViewFragmentB 的片段时:

getFragmentManager().beginTransaction()
    .add(R.id.fragment_container, FragmentB)
    .commit()

我注意到 FragmentAListViewgetView() 仍然被调用,即使顶部片段现在是 FragmentB,任何原因以及我如何防止这种行为?

您正在将碎片堆叠在一起。

而不是 add 使用 replace 替换该容器中的任何现有片段。