Is Fragment.setUserVisibleHint()
method called by the System , or
do we call it manually ?
3 Answers
Looking at the sources, it looks like this system is mostly intended for fragments put in a pager.
You (or the implementation of the fragment pager for example) should set it as a hint ("Hint provided by the app" as a comment in the Fragment
source says) so that it can, for example, defer its loading (initialization) if not visible and prioritize the loading of visible fragments (typical need when used into a pager, again).
Note though, that the FragmentPagerAdapter makes use of this and properly calls setUserVisibleHint()
on its fragments, which is why I guess you see some people advising to e.g. override setUserVisibleHint()
to know when a fragment gets visible to the user or not (and this would thus only work when the fragment is inside a FragmentPagerAdapter
, not when just put in a usual activity layout for example).
So to answer the question clearly: you call it manually, and FragmentPagerAdapter
also calls it manually.

- 7,968
- 2
- 24
- 26
-
1So, what is the alternative to this, when a fragment is added in a normal activity.? – AndroidGuy Mar 24 '17 at 09:33
-
1How to call it manually ? Should we check return values of isAdded(), isHidden() .. ? isVisible() always returns false. – AndroidGuy Mar 24 '17 at 09:40
-
@AndroidGuy Unless you implement a class which manages fragments (like a fragment pager for example), you **don't need** to call `Fragment#setUserVisibleHint`. So if you simply add a fragment to an activity, you don't need to bother about it. To get better help, I suggest you clarify what you *want to do* (your problem) and search for it on StackOverflow, or ask a new question if none matches your problem perfectly. – desseim Mar 24 '17 at 19:45
-
1I wanted something which would tell me that my fragment is visible to the user. Your answer makes things clear. I have upvoted :) This worked when I was using a FragmentPagerAdapter. But, When my fragment was in an activity, without view pager, I checked the return value of getUserVisibleHint() & solved my issue. Thanks, anyways. – AndroidGuy Mar 27 '17 at 05:42
From the Documentation:
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.
An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.
Parameters isVisibleToUser true if this fragment's UI is currently visible to the user (default), false if it is not.
Sound like it's defaulted to True and you can set it to False if you would want.

- 3,411
- 2
- 28
- 28
Not for Android less than version 15 and Android Support Library less than revision 11.

- 13,196
- 5
- 87
- 72
-
2This is part of the support v4 library and I'm using it right now on a Nexus One, running API 10, without any problems. There doesn't seem to be any reason why this won't run all the way down to API 4. – gMale May 19 '15 at 05:15