Is Fragment.setUserVisibleHint() called by the android System?
Asked Answered
E

3

25

Is Fragment.setUserVisibleHint() method called by the System , or
do we call it manually ?

Eratosthenes answered 6/2, 2013 at 14:34 Comment(0)
M
41

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.

Mantic answered 18/12, 2013 at 11:32 Comment(4)
So, what is the alternative to this, when a fragment is added in a normal activity.?Threonine
How to call it manually ? Should we check return values of isAdded(), isHidden() .. ? isVisible() always returns false.Threonine
@Threonine 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.Mantic
I 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.Threonine
P
0

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.

Documentation

Pterodactyl answered 6/2, 2013 at 18:10 Comment(1)
Had read that.. It doesn't talk about the system. Thanks anyway.Eratosthenes
G
0

Not for Android less than version 15 and Android Support Library less than revision 11.

Gent answered 7/7, 2014 at 0:39 Comment(1)
This 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.Edric

© 2022 - 2024 — McMap. All rights reserved.