问题:
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