Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 864
vivek Avatar asked Feb 06 '13 14:02

vivek


People also ask

What is a fragment in Android?

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.

How do you know if a fragment is visible?

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.

What is retained fragment in Android?

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.

How can I tell if an Android fragment is destroyed?

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.


1 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.

like image 183
desseim Avatar answered Oct 29 '22 01:10

desseim