/**
* 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.
*
* <p>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.</p>
*
* @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
* false if it is not.
*/
public void setUserVisibleHint(boolean isVisibleToUser) {
if (!mUserVisibleHint && isVisibleToUser && mState < STARTED
&& mFragmentManager != null && isAdded()) {
mFragmentManager.performPendingDeferredStart(this);
}
mUserVisibleHint = isVisibleToUser;
mDeferStart = mState < STARTED && !isVisibleToUser;
}
其中有一句話是這么說(shuō)的:This may be used by the system to prioritize operations such as fragment lifecycle updates
這個(gè)可能被用來(lái)在一組有序的fragmen里 忿檩,例如 fragment生命周期的更新重抖。告訴我們這個(gè)方法被調(diào)用希望在一個(gè)pager里烘浦,因此 FragmentPagerAdapter 可以使用這個(gè),然后調(diào)用這個(gè) setUserVisibleHint() 在fragment里浆熔,因此有些人也建議重寫 setUserVisibileHint() 來(lái)知道當(dāng)前一個(gè)fragment對(duì)用戶來(lái)說(shuō)是隱藏還是顯示蛉幸,這個(gè)方法僅僅工作在FragmentPagerAdapter中张抄,不能被使用在一個(gè)普通的activity中革半。
因此我這個(gè)單個(gè)fragment 就是不被調(diào)用的 碑定,可以重寫 onHiddenChanged() 方法來(lái)得知當(dāng)前 fragment 的狀態(tài)流码。