Is Fragment.setUserVisibleHint()
method called by the System , or
do we call it manually ?
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.
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. –
Mantic 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.
Not for Android less than version 15 and Android Support Library less than revision 11.
© 2022 - 2024 — McMap. All rights reserved.