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. 尝试add而不是replace fragments (编辑)

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

最终我追查下来,我重写onSaveInstanceState()在我的活动,但没有调用超级。 只要我修正了findFragmentByTag()返回的Fragment如预期。

您可以使用

 fragmentTransaction.addToBackStack(yourFragmentTag); 

之后,您可以重复使用

 getSupportFragmentManager().findFragmentByTag(yourFragmentTag); 

我很长时间以来对此感到困惑。 首先,您需要将您要更换的碎片推入后面的堆栈。 你提供的标签放在你正在添加的片段上,而不是你回到后面的那个。 后来,当你把它推到后面的堆栈时,这个标签就随之而来。 这里的代码包含对象,以便于追踪。 您必须在“提交”之前调用“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" 

在片段事务之后调用getSupportFragmentManager()。executePendingTransactions()。

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

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

在我的情况下,我不得不创build一个类级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(); } } }