How to test if a fragment view is visible to the user?

前端 未结 10 565
闹比i
闹比i 2020-11-28 04:42

I have a ViewPager, each page is a Fragment view. I want to test if a fragment is in a visible region. the Fragment.isVisible only test

  • the fragment is attache
10条回答
  •  误落风尘
    2020-11-28 05:18

    setUserVisibleHint probably may not be called, onHiddenChanged may be called not every time when another fragment is being closed. So, you may rely on onResume (and onPause), but it is usually called too often (for example, when you turn on a device screen). Also in some situations it is not called, you should manage current fragment in host activity and write:

    if (currentFragment != null) {
        currentFragment.onResume();
    }
    

提交回复
热议问题