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

前端 未结 7 2120
隐瞒了意图╮
隐瞒了意图╮ 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:33

    I resolved with this:

    public ArrayList getAllFragments() {
        ArrayList lista = new ArrayList();
    
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            try {
                fragment.getTag();
                lista.add(fragment);
            } catch (NullPointerException e) {
    
            }
        }
    
        return lista;
    
    }
    

提交回复
热议问题