Android GetFragmentManager.findFragmentByTag() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23581894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:17:04  来源:igfitidea点击:

GetFragmentManager.findFragmentByTag() returns null

androidandroid-fragmentsfragment

提问by John Moffitt

        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);

I've supplied a tag so I'm not sure why findFragmentByTag()returns null.

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

What I've tried from reading other questions:

我从阅读其他问题中尝试过的内容:

  1. this.setRetainInstance(true)in the oncreateof both fragments.

  2. Both fragmentconstructors are empty public fragmentName(){}.

  3. tried executePendingTransactionsafter adding the fragments.

  4. tried addinstead of replaceon the fragments(edited)

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

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

  3. 试图executePendingTransactions加入之后fragments

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

采纳答案by tagy22

I was having the same problem of findFragmentByTag()always returning null.

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

Eventually I tracked it down, I was overriding onSaveInstanceState()in my Activity but not calling super. As soon as I fixed that findFragmentByTag()returned the Fragment as expected.

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

回答by Rick Shory

I was confused about this for a long time. First, you need to save the fragment you are replacing by pushing it onto the back stack. The tag you supply is put on the fragment you are adding, not the one you are pushingonto the back stack. Later, when you do push it onto the back stack, that tag goes with it. Here's code with objects broken out to make it easier to trace. You must call 'addToBackStack' before '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"

回答by Ali Gürelli

Call getFragmentManager().executePendingTransactions() after fragment transaction.

在片段事务之后调用 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");

回答by savepopulation

You can use

您可以使用

fragmentTransaction.addToBackStack(yourFragmentTag);

After that you can reuse it with

之后你可以重用它

getSupportFragmentManager().findFragmentByTag(yourFragmentTag);

回答by Dmitry Dudá

Answered here, just need to call getSupportFragmentManager().executePendingTransactions();after your findByTag or findById

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

回答by Prabin Timsina

In my case I had to create a class level FragmentManager object and then use it instead of using getSupportFragmentManager() directly.

就我而言,我必须创建一个类级别的 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();
    }
  }
}