5

It looks like it's possible to get all fragments of an Activity pretty easily. But how can I get all subfragments for a given fragment ?

This question is also related to getParentFragment API 16

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173

2 Answers2

0

You can do it in the same way -- just use the FragmentManager obtained using the Fragment instance's getChildFragmentManager() instead of the Activity FragmentManager. Of course, this assumes you're using a recompiled version of the support library with getFragments() not hidden, or are using reflection to get invoke that method.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Same way as what ? Moreover, I am looking for some method that will always work, it's not for an app but for a library that should work for all apps. – Snicolas Jun 20 '14 at 06:13
  • Same as the answer you linked to (using getFragments()). I don't believe there's any other way to do so. – Kevin Coppock Jun 20 '14 at 06:14
  • It looks like getFragments got public on support fragment manager, even if javadoc doesn't say so. And yes, using reflection is mandatory for the native fragment manager. – Snicolas Jun 20 '14 at 06:21
  • But this doesn't address the case where I use native (non support) fragments. – Snicolas Jun 20 '14 at 06:37
0

The following solution is not perfect but it works in some extent :

  • If Android SDK is 17+, then it works fine
  • below SDK 17 it works for fragments at the root level (added directly to activity), and also works fine for fragments of level 1 (added to a fragment at root level).
  • for fragments of level >= 2, then it will always return a fragment of root level. It means that it is not possible to return the real parent of a fragment whose level is >=2, it will always return its ancestor at the root level.
  • And, unfortunately, it means you must have access to the activity class, so this solution is not really generic.

Here is the solution. The MyActivity class is given below.

public static Fragment getParentFragment(Fragment fragment) {
    if( Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
        return fragment.getParentFragment();
    MyActivity activity = (MyActivity)fragment.getActivity();
    List<Fragment> fragmentList = activity.getActiveFragments();
    if( fragmentList.contains( fragment) ) {
        return null;
    }
    for( Fragment fragmentLevel1 : fragmentList ) {
        if( fragmentLevel1.getFragmentManager() == fragment.getFragmentManager() ) {
            return fragmentLevel1;
        }
    }
    //this is not supposed to happen, it might be better to throw an exception
    return null;
}

Where MyActivity is based on : Is there a way to get references for all currently active fragments in an Activity?

public class MyActivity {
    List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>();

    @Override
    public void onAttachFragment (Fragment fragment) {
        fragList.add(new WeakReference(fragment));
    }

    public List<Fragment> getActiveFragments() {
        ArrayList<Fragment> ret = new ArrayList<Fragment>();
        for(WeakReference<Fragment> ref : fragList) {
            Fragment f = ref.get();
            if(f != null) {
                if(f.isVisible()) {
                    ret.add(f);
                }
            }
        }
        return ret;
    }
}
Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173