问题描述
当使用 ViewPager
和片段时,我们的 onPause
、onResume
方法在标签之间移动时不会被调用.当我们变得可见或被隐藏时,有什么方法可以在片段中找出?
When using ViewPager
with fragments, our onPause
, onResume
methods are not called when moving between tabs. Is there any way we can figure out in the fragment when we're being made visible or being hidden?
不幸的是,我在 onResume
、onPause
中有逻辑,例如向位置服务注册,在切换标签时永远不会停止,因为 onPause
永远不会调用直到退出整个应用程序.
Unfortunately I have logic in onResume
, onPause
, like registering with location services, that never get stopped when switching tabs because onPause
never gets called until one exits the whole app.
推荐答案
ViewPager 自带 OnPageChangeListener
接口.通过为之前和当前显示的页面设置一些标志,您可以模拟这种行为.
The ViewPager comes with the OnPageChangeListener
interface. By setting some flags for the previous and currently shown pages, you can emulate this behavior.
这篇关于带有片段的 ViewPager - onPause()、onResume()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!