Fragment可見性的判斷-——-getUserVisibleHint-和-isVisible-的區(qū)別

isVisible: 是關(guān)聯(lián)與fragment生命周期的可見性的判斷
User Visible Hint: 當(dāng)fragment的可見性與其生命周期沒有關(guān)系的時候,來進(jìn)行對于可見性的判斷

在使用viewPager進(jìn)行添加若干fragment的時候,當(dāng)parentActivity進(jìn)行onStart()的時候,里面所有的fragment都會進(jìn)行onStart(),即只要在Parent的可見狀態(tài)之中,內(nèi)部的所有的fragmentisVisible()都是true,所以在viewPager的時候,是根據(jù)當(dāng)前的fragment是否被選中去判斷其的顯示與隱藏,而當(dāng)前這個fragment是否被選中的標(biāo)志在ViewPager中就是UserVisibleHint的值。

isVisible() 是使用FragmentManager進(jìn)行管理,是對于當(dāng)前fragment是否對用戶“可見”的判定方式。這里的“可見”是指在onStart()onStop()之間。
調(diào)用FragmentManager.beginTransaction().add/remove/replace()等相關(guān)方法,會導(dǎo)致Fragment被添加和移除,從而改變isVisible()的返回值

setUserVisibleHint(boolean isVisibleToUser) 是當(dāng)使用viewPager去管理若干fragment的時候進(jìn)行對于某fragment在顯示跟隱藏的時候回調(diào)的方法
setUserVisibleHint()從名字來看是給使用Fragment的模塊來調(diào)用的,app也可以調(diào)用這個方法。

void setUserVisibleHint (boolean isVisibleToUser)

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.
Note: This method may be called outside of the fragment lifecycle. and thus has no ordering guarantees with regard to fragment lifecycle method calls.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容