10

I'm trying to debug some code and was hoping to list the fragments in the current backstack in Android. The stock solution is to simply list them from the support manager, so from a method within an activity :

Log.i("fragments","Count : " + supportFragmentManager.fragments.toString())

However using this along with Android's navigation component simply lists the navigation host fragment.

... I/fragments: [NavHostFragment{b1a2d20 (f8ec2420-b534-42df-89f0-0cb189610a26) id=0x7f080001}]

Some of the answers on SO e.g. Sergii Suvorov provide the following alternative :

Log.i("fragments","Count : " + supportFragmentManager.findFragmentById(R.id.NAVIGATION)?.childFragmentManager?.fragments.toString())

Which simply returns null for me

... I/fragments: null

Within a method of a fragment I can call the following

Log.i("fragments", fragmentManager?.fragments.toString())

and it does return the desired result

... I/fragments: [FirstFragment{1475645 (3bb8566b-22b0-4c98-82ea-566e18915145) id=0x7f080001}]

I do not however see how to do this from the activity, perhaps some one out there knows the magic incantation ?

2
  • Hi @Anatolii, thank you for your speedy response, the android work is a moonlight project, hence my slow response. I'm afraid your answer does not work and is the reason I posted my original question. When using the android navigation components the navigation component becomes the only item upon the activities' back stack. The navigation component itself may have N or so items upon it's own backstack by comparison.
    – Carel
    May 27, 2019 at 21:27
  • I think there must be some means of interrogating the navigation fragments' stack directly but I haven't seen the appropriate method. findNavigationController does not appear to have an attribute that would return a backstack of sorts from what I can see and the "sergei" method returns null for me.
    – Carel
    May 27, 2019 at 21:34

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.