hello I am having an activity which have like 10 fragments I want to check when there is no fragment visible i.e after pressing back button I return to the main activity I want catch this back not back between fragments
where no fragment visible
I found a way here
How to know if a Fragment is Visible?
to check by tag name like here but I can't check for all 10 fragments by tag name
any help?
Asked
Active
Viewed 584 times
1
-
4if you found this - getSupportFragmentManager().getBackStackEntryCount() == 0 means not fragment is currently visible. – Sanwal Singh Jan 03 '17 at 12:22
-
thanks a lot it works with getFragmentManager().getBackStackEntryCount() ==0 as I extends activity – Radwa Jan 03 '17 at 12:30
-
For Kotlin, you can use supportFragmentManager.backStackEntryCount – portfoliobuilder Jan 24 '20 at 23:45
1 Answers
5
If I understood your question right, by using
if (getSupportFragmentManager().findFragmentById(R.id.some_layout_id) != null)
you can find out if the layout with that id contains a fragment or not. This way you don't need to check for each fragment and you can check the fragment container directly. I hope it helps!

Matan Koby
- 232
- 1
- 11