android – 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.

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

> this.setRetainInstance(true)在两个片段的oncreate上.
>两个片段构造函数都是空的public fragmentName(){}.
>在添加片段后尝试执行executePendingTransactions.
>尝试添加而不是替换片段(已编辑)

解决方法

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

最终我跟踪它,我在我的活动中超越了SaveInstanceState(),但没有调用超级.一旦我修复了findFragmentByTag()返回的Fragment按预期.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...