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

前端 未结 7 2129
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 06:44

    android.support.v4.app.FragmentManager has a method called getFragments() which does exactly what you need, but it was never accessible. Suddenly, though, it is, but I'm not sure if that's intended because it's still marked as hidden.

    /**
     * Get a list of all fragments that have been added to the fragment manager.
     *
     * @return The list of all fragments or null if none.
     * @hide
     */
    public abstract List getFragments();
    

    The regular android.app.FragmentManager doesn't have this method at all, but if really needed, you can access the same object by getting the field mActive via reflection (be careful there: starting with API 26, its type is a SparseArray instead of List). Use at own risk :)

提交回复
热议问题