getSupportFragmentManager().getFragments() shows a compile time error

前端 未结 3 868
小蘑菇
小蘑菇 2020-12-05 04:14

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

getSupportFragmentManager().getFrag

相关标签:
3条回答
  • 2020-12-05 04:36

    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.

    0 讨论(0)
  • 2020-12-05 04:37

    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();
    
    0 讨论(0)
  • 2020-12-05 04:48

    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()

    0 讨论(0)
提交回复
热议问题