43

Calling getSupportFragmentManager().getFragments() shows a compile time error with the message below:

getSupportFragmentManager().getFragments() can only be called from within the same library group(groupId = com.android.support)

I have imported the following classes in MainActivity:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

MainActivity extends AppCompatActivity.

My project module level build.gradle file is as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.mycompany.floatingdemo"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:support-vector-drawable:25.2.0'
    testCompile 'junit:junit:4.12'
}

This is the source code for the method getFragments inside FragmentManager.java.

/**
 * 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
 */
@RestrictTo(LIBRARY_GROUP)
public abstract List<Fragment> getFragments();

I have recently updated my Android Studio to the latest stable version (2.3) and updated the Android Gradle plugin as well. I think this may be relevant because I have not previously seen this error.

stkent
  • 19,772
  • 14
  • 85
  • 111
Er. Kaushik Kajavadara
  • 1,657
  • 2
  • 16
  • 37

3 Answers3

18

As noticeable in the FragmentManager documentation, getFragments() is not a public method available to apps, but an internal implementation detail of the Support Library, hence the use of the RestrictTo annotation that was added to prevent usage of private APIs.

You'll want to change your code to not use getFragments and only use the public APIs.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 2
    Yeah, but what could be the reason behind this restriction? – Paresh P. Mar 03 '17 at 06:58
  • 5
    Since `getFragments()` is now restricted, what is the "public API' for getting a list of all `Fragments` currently added to the `FragmentManager`? – jenzz Mar 03 '17 at 12:09
  • 2
    @jenzz - there's no API for that. You could certainly register a [FragmentLifecycleCallbacks](https://developer.android.com/reference/android/support/v4/app/FragmentManager.FragmentLifecycleCallbacks.html) and keep your own list. – ianhanniballake Mar 03 '17 at 19:00
  • 5
    this makes little sense. hasn't this been working so far? – CptEric Mar 13 '17 at 15:45
  • @CptEric - it has always been a private API. – ianhanniballake Mar 13 '17 at 16:35
  • @ianhanniballake what if we still use this private method? As it's running properly even though it's showing compile time error. Are there any cons of using it? – Nikhil Mar 14 '17 at 05:17
  • @indramurari my assumption would be that the method will eventually become entirely inaccessible eventually. I would go ahead and implement your own methods to do this right now just to be safe. – Vic Vuci Mar 14 '17 at 13:40
  • 2
    Why on Earth is `FragmentLifecycleCallbacks` abstract and not an interface? – Alex Burdusel Mar 18 '17 at 15:02
  • @ianhanniballake In my project I dont see FragmentLifecycleCallbacks at all under android.support.v4.app.FragmentManager. Any idea why this might be the case? – Ashok Koyi Mar 19 '17 at 08:17
  • 1
    @Kalinga - make sure you are using [25.2.0](https://developer.android.com/topic/libraries/support-library/revisions.html#25-2-0) or higher – ianhanniballake Mar 19 '17 at 12:19
  • 1
    While the non-support FragmentManager API seems to have the method as private, the `android.support.v4.app.FragmentManager` method is still public and appears to be part of the public API but I am also receiving the same error as OP (and experiencing the same behaviour). Could this perhaps be a bug? – henrycjc May 18 '17 at 05:12
  • I use it like this: for (Fragment fragment : getSupportFragmentManager().getFragments()) { fragment.onActivityResult(requestCode, resultCode, data); } and got a npe which says the fragment is null. I don't know why. – Allen Vork May 31 '17 at 10:02
6

Alternative For those who may be using getFragments() in their coding, I have replaced my code to get last fragment in backstack to this code(I am using this code in onBackPressed() to apply changes according to currentFragment assumed all fragments are added to backstack):

FragmentManager.BackStackEntry backStackEntryAt = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1);
currentFragment = backStackEntryAt.getName();
Er. Kaushik Kajavadara
  • 1,657
  • 2
  • 16
  • 37
3

You can use (make sure using 25.2.0 or higher )

supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentManager.FragmentLifecycleCallbacks() {
    override fun onFragmentAttached(fm: FragmentManager?, f: Fragment?, context: Context?) {
        f?.let { fList.add(it) }
    }

    override fun onFragmentDetached(fm: FragmentManager?, f: Fragment?) {
        f?.let { fList.remove(it) }
    }

}, false) 

Instead of using getFragments()