繁体   English   中英

findFragmentByTag始终返回null

[英]findFragmentByTag always returns null

我要去哪里错了。 我只是想检查具有给定ProductId的片段在BackStack中是否已经可用;

如果是,则不添加新的片段,否则将当前片段添加到后台。

public void showThisFragment(Fragment newFragment,int productId){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.am_fragment_holder, newFragment);
    if(fragmentManager.findFragmentByTag(productId+"")==null){
        fragmentTransaction.addToBackStack(productId+"");
    }else{
        //TODO fragment already present
        //So dont add to the back stack
    }    
    fragmentTransaction.commit();
}

请帮忙!

没有标签添加到替换的片段。 您应该使用三个参数替换方法。 我没有测试它,所以告诉我我是否正确。

fragmentTransaction.replace(R.id.am_fragment_holder, newFragment, String.valueOf(productId));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM