GetFragmentManager.findFragmentByTag()返回null

Joh*_*itt 18 android fragment android-fragments

        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);
Run Code Online (Sandbox Code Playgroud)

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

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

  1. this.setRetainInstance(true)oncreate两者中fragments.

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

  3. executePendingTransactions加入后尝试了fragments.

  4. 尝试add而不是replacefragments(编)

小智 16

很长一段时间我对此感到困惑.首先,您需要将要替换的片段保存到后台堆栈中.您提供的标签放在要添加的片段上,而不是您要送到后台堆栈的片段上.之后,当你把它推到后面的堆栈上时,那个标签就随之而来.这里的代码分解为对象,以便于跟踪.你必须在'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"
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,因为需要在替换/添加方法中添加TAG而不是addToBackStack(TAG)方法,因为它与findFragmentByTag(TAG)无关. (3认同)

tag*_*y22 15

我遇到了findFragmentByTag()总是返回null的问题.

最终我追踪了它,我onSaveInstanceState()在我的活动中压倒一切,但没有打电话给超级.一旦我修复了,findFragmentByTag()就像预期的那样返回了Fragment.

  • 对我不起作用。并且不能与@Dmitry Dudá 的答案混合使用。我没有使用过 `addToBackStack`。 (2认同)
  • 没有帮助。我不会推翻它。 (2认同)

Ali*_*lli 8

片段事务后调用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");
Run Code Online (Sandbox Code Playgroud)

  • 您应该在答案中添加一些解释/背景信息,以使其更有价值.(并将`getSupport ...`放在反引号中``) (3认同)

sav*_*ion 7

您可以使用

fragmentTransaction.addToBackStack(yourFragmentTag);
Run Code Online (Sandbox Code Playgroud)

之后,您可以重复使用它

getSupportFragmentManager().findFragmentByTag(yourFragmentTag);
Run Code Online (Sandbox Code Playgroud)

  • 我也有同样的问题,我使用addToBackStack(TAG),但问题是添加(容器,片段).我已将其更改为添加(容器,片段,TAG)并且问题消失了 (2认同)

归档时间:

查看次数:

20846 次

最近记录:

7 年,4 月 前