GetFragmentManager.findFragmentByTag()返回null

getFragmentManager().beginTransaction() .replace(R.id.graph_fragment_holder, new GraphFragment(), "GRAPH_FRAGMENT") .commit(); getFragmentManager().beginTransaction() .replace(R.id.list_fragment_holder, new ListFragment(), "LIST_FRAGMENT") .commit(); //getFragmentManager().executePendingTransactions(); GraphFragment graphFragment = (GraphFragment) getFragmentManager().findFragmentByTag("GRAPH_FRAGMENT"); graphFragment.setData(data); ListFragment listFragment = (ListFragment) getFragmentManager().findFragmentByTag("LIST_FRAGMENT"); listFragment.setData(data); 

我提供了一个标签,所以我不确定为什么findFragmentByTag()返回null

我在阅读其他问题时尝试过:

  1. this.setRetainInstance(true)在两个fragmentsoncreate中。

  2. 两个fragment构造函数都是空的public fragmentName(){}

  3. 添加fragments后尝试执行executePendingTransactions

  4. 尝试在fragmentsadd而不是replace (已编辑)

我有同样的问题, findFragmentByTag()总是返回null。

最终我跟踪了它,我在我的Activity中覆盖了onSaveInstanceState()但没有调用super。 一旦我修复了findFragmentByTag()就按预期返回了Fragment。

很长一段时间我对此感到困惑。 首先,您需要将要更换的片段保存到后台堆栈中。 您提供的标签放在要添加的片段上,而不是放在后面堆栈上的标签上。 之后,当你把它推到后面的堆栈上时,那个标签随之而来。 这里的代码分解为对象,以便于跟踪。 你必须在’commit’之前调用’addToBackStack’。

 GraphFragment grFrag = new GraphFragment(); FragmentTransaction tr = getSupportFragmentManager().beginTransaction(); tr.replace(R.id.fragment_container, grFrag, "GRAPH_FRAGMENT"); // grFrag is about to become the current fragment, with the tag "GRAPH_FRAGMENT" tr.addToBackStack(null); // 'addToBackStack' also takes a string, which can be null, but this is not the tag tr.commit(); // any previous fragment has now been pushed to the back stack, with it's tag ListFragment liFrag = new ListFragment(); FragmentTransaction tr = getSupportFragmentManager().beginTransaction(); tr.replace(R.id.fragment_container, liFrag, "LIST_FRAGMENT"); // liFrag is is about to become the current fragment, with the tag "LIST_FRAGMENT" tr.addToBackStack(null); tr.commit(); // 'grFrag' has now been pushed to the back stack, with it's tag being "GRAPH_FRAGMENT" 

您可以使用

 fragmentTransaction.addToBackStack(yourFragmentTag); 

之后,您可以重复使用它

 getSupportFragmentManager().findFragmentByTag(yourFragmentTag); 

片段事务后调用getFragmentManager()。executePendingTransactions()。

 getFragmentManager() .beginTransaction() .replace(R.id.container, new ExampleFragment(), "YOUR TAG HERE"); .commit(); //after transaction you must call the executePendingTransaction getFragmentManager().executePendingTransactions(); //now you can get fragment which is added with tag ExampleFragment exampleFragment = getFragmentManager().findFragmentByTag("YOUR TAG HERE"); 

回答这里 ,只需要调用getSupportFragmentManager().executePendingTransactions(); 在你的findByTag或findById之后

在我的情况下,我必须创建一个类级别FragmentManager对象,然后使用它而不是直接使用getSupportFragmentManager()。

 public class Main extends BaseActivity { FragmentManager fragmentManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragmain); fragmentManager = getSupportFragmentManager(); initFrag1(); } private void initFrag1() { String name = Frag1.class.getSimpleName(); if (fragmentManager.findFragmentByTag(name) == null) { fragmentManager.beginTransaction() .add(R.id.frag_container, new Frag1(), name) .addToBackStack(name) .commit(); } } }