get the latest fragment in backstack

后端 未结 17 2053
长发绾君心
长发绾君心 2020-11-27 11:24

How can I get the latest fragment instance added in backstack (if I do not know the fragment tag & id)?

FragmentManager fragManager = activity.getSupport         


        
17条回答
  •  执笔经年
    2020-11-27 12:08

    The answer given by deepak goel does not work for me because I always get null from entry.getName();

    What I do is to set a Tag to the fragment this way:

    ft.add(R.id.fragment_container, fragmentIn, FRAGMENT_TAG);
    

    Where ft is my fragment transaction and FRAGMENT_TAG is the tag. Then I use this code to get the fragment:

    Fragment prev_fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG);
    

提交回复
热议问题