I am working with a viewpager and compiling API 23. I'm showing a compile error in my code for the following statement, but the project does compile.
List<Fragment> fragments = getSupportFragmentManager().getFragments();
Furthermore, I cannot find this method in the android documentation for either the support fragment manager and the non-support one. Does anyone know what's going on here?
asked Aug 4, 2016 at 16:17
Kristy Welsh
8,44913 gold badges74 silver badges118 bronze badges
1 Answer 1
I actually did this to get a reference to all the fragments:
private List<WeakReference<Fragment>> mFragList = new ArrayList<WeakReference<Fragment>>();
@Override
public void onAttachFragment (Fragment fragment) {
mFragList.add(new WeakReference(fragment));
}
public List<Fragment> getActiveFragments() {
ArrayList<Fragment> ret = new ArrayList<Fragment>();
for(WeakReference<Fragment> ref : mFragList) {
Fragment f = ref.get();
if(f != null) {
if(f.isVisible()) {
ret.add(f);
}
}
}
return ret;
}
public Fragment findFragement(String tClass) {
List<Fragment> fragments = getActiveFragments();
for (Fragment fragment : fragments) {
if (tClass.equalsIgnoreCase("Home")) {
if (fragment instanceof ToggleFragment) {
return fragment;
}
} else if (tClass.equalsIgnoreCase("Contacts")) {
if (fragment instanceof ContactFragment) {
return fragment;
}
}
}
return null;
}
answered Aug 10, 2017 at 14:11
Kristy Welsh
8,44913 gold badges74 silver badges118 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Kristy Welsh
@MarissaNicholas I don't think it would create duplicates when the screen is recreated (like on rotate). Maybe on ft.replace().
do you think something like this would prevent that from happening? : @Override public void onAttachFragment(Fragment fragment) { for (WeakReference<Fragment> reference : mFragList) { if (reference instanceOf WeakRefence<ThisFragment>) { return; } mFragList.add(fragment); } }
default
We shouldn't use this method. It is marked with @hide and was not supposed to be included with the support library jar. It should not be considered part of the exported API.Since it was hidden, I guess that Google finally decided to remove that method.