【问题标题】:GetFragmentManager.findFragmentByTag() returns nullGetFragmentManager.findFragmentByTag() 返回 null
【发布时间】:2014-06-28 05:07:47
【问题描述】:
        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)oncreatefragments 中。

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

  3. 在添加fragments 后尝试executePendingTransactions

  4. fragments 上尝试了add 而不是replace(已编辑)

【问题讨论】:

标签: android android-fragments fragment


【解决方案1】:

我为此困惑了很长时间。首先,您需要通过将要替换的片段推入后堆栈来保存它。您提供的标签放在您正在添加的片段上,而不是您正在到后堆栈的片段上。稍后,当您确实将其推送到后堆栈时,该标签随之而来。这是带有分解对象的代码,以便于跟踪。您必须在 '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"

【讨论】:

  • 谢谢。替换中提供的字符串标记和 backstack 标记之间的区别是两个不同的东西。这让我绊倒了一段时间
  • 这是正确的答案,因为 TAG 需要在 replace/add 方法中添加,而不是 addToBackStack(TAG) 方法,因为它与 findFragmentByTag(TAG) 无关。
  • 很好的解释。救了我哥们
【解决方案2】:

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

最终我找到了它,我在我的活动中覆盖了onSaveInstanceState(),但没有调用超级。一旦我修复了findFragmentByTag() 按预期返回的片段。

【讨论】:

  • 对我不起作用。并且不能与@Dmitry Dudá 答案混合使用。我没用过addToBackStack
  • 没有帮助。我没有覆盖它。
【解决方案3】:

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

【讨论】:

  • 您应该在答案中添加一些解释/背景信息,以使其更有价值。 (并把getSupport...放在反引号``)
  • androidX 对我没有帮助
  • 这行得通,但更好的解释将不胜感激,有一天它在没有这个的情况下工作,第二天就需要它。
【解决方案4】:

你可以使用

fragmentTransaction.addToBackStack(yourFragmentTag);

之后你可以重复使用它

getSupportFragmentManager().findFragmentByTag(yourFragmentTag);

【讨论】:

  • getSupportFragmentManager() 不解决问题因为他可能不会从android.support.v4.app.Fragment 扩展成两个片段
  • 他可能也不需要将他的片段添加到后台堆栈
  • @ahmedhamdy 当然。这是一个快速的答案。如果他不使用支持库,他可以使用 getFragmentManager();他可以添加所有想要重用的片段并找到它们并在以后重用。
  • addtoBackStack 是我所缺少的:-P
  • 我也遇到了同样的问题,我用了addToBackStack(TAG),但是问题出在add(container, fragment)。我已将其更改为 add(container, fragment, TAG) 并且问题消失了
【解决方案5】:

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

【讨论】:

    【解决方案6】:

    在我的例子中,我必须创建一个类级别的 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();
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2020-06-19
      • 2011-10-15
      • 2012-08-16
      相关资源
      最近更新 更多