問題:
Problem: Fragment onResume()
in ViewPager
is fired before the fragment becomes actually visible.問題: ViewPager
片段onResume()
在片段實際可見之前被觸發。
For example, I have 2 fragments with ViewPager
and FragmentPagerAdapter
.例如,我有 2 個帶有ViewPager
和FragmentPagerAdapter
。 The second fragment is only available for authorized users and I need to ask the user to log in when the fragment becomes visible (using an alert dialog).第二個片段僅對授權用戶可用,我需要讓用戶在片段可見時登錄(使用警報對話框)。
BUT the ViewPager
creates the second fragment when the first is visible in order to cache the second fragment and makes it visible when the user starts swiping.但是ViewPager
在第一個片段可見時創建第二個片段,以緩存第二個片段並在用戶開始滑動時使其可見。
So the onResume()
event is fired in the second fragment long before it becomes visible.所以onResume()
事件在第二個片段變得可見之前很久就會被觸發。 That's why I'm trying to find an event which fires when the second fragment becomes visible to show a dialog at the appropriate moment.這就是爲什麼我試圖找到一個事件,當第二個片段變得可見以在適當的時候顯示對話框時會觸發該事件。
How can this be done?如何才能做到這一點?
解決方案:
參考一: https://en.stackoom.com/question/g3tL參考二: https://stackoom.com/question/g3tL