Is there a way to get references for all currently active fragments in an Activity?

前端 未结 7 2087
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 05:46

I haven\'t found a simple way to get all currently active (visible, currently in Resumed state) Fragments in an Activity. Is it possible without custom bookkeeping in my Act

相关标签:
7条回答
  • 2020-11-28 06:52

    we can use some irregular-but-legal method

        ArrayList<Fragment> getActiveFragments() {
            Fragment f;
            final ArrayList<Fragment> fragments = new ArrayList<>();
            int i = 0;
            try {
                while ((f = getFragmentManager().getFragment(new Intent().putExtra("anyvalue", i++).getExtras(), "anyvalue")) != null) {
                    fragments.add(f);
                }
            } catch (IllegalStateException ex) {
    
            }
            return fragments;
        }
    
    0 讨论(0)
提交回复
热议问题