Is Fragment.setUserVisibleHint()
method called by the System , or
do we call it manually ?
A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.
fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible. To enable this behavior in the first ViewPager you have to pass FragmentPagerAdapter.
A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.
Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With