How to know if a Fragment is Visible?
I'm using the support library v4 and my questions are, How to know if a Fragment is Visible? and How can I change the propierties of the Layout inflated in the Fragment? Thanks in advance.
---Edit---
I'm using fragments like in the android developers tutorial with a FragmentActivity
You should be able to do the following:
MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
//DO STUFF
}
else {
//Whatever
}
Both isVisible()
and isAdded()
return true
as soon as the Fragment
is created, and not even actually visible. The only solution that actually works is:
if (isAdded() && isVisible() && getUserVisibleHint()) {
// ... do your thing
}
This does the job. Period.
NOTICE: getUserVisibleHint() is now deprecated. be careful.