3

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
7
  • The first comment to Michael's answer says it all... stackoverflow.com/questions/6102007/… 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. Commented Aug 4, 2016 at 16:25
  • Did you use import android.support.v4.app.Fragment;? Commented Aug 4, 2016 at 16:26
  • That method has not been deprecated as of version 24.1.1 of the support library. What exactly is the error being displayed? Commented Aug 4, 2016 at 16:29
  • @Tanis.7x Cannot resolve method. (Basically just getting a red underline under the method, but the project compiles. Commented Aug 4, 2016 at 16:56
  • @sonnv1368, yes, I did. Commented Aug 4, 2016 at 16:58

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much kristy! you are the best! Also , if you can explain what getactivefragments does, it will be awesome!
also for : @Override public void onAttachFragment (Fragment fragment) { mFragList.add(new WeakReference(fragment)); } wouldnt it cause duplicates when a screen is recreated or ft.replace() is used.
@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); } }

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.