Check if fragment is currently visible or no

前端 未结 10 813
悲&欢浪女
悲&欢浪女 2020-12-05 03:10

I know there are lots of similar questions in StackOverflow but my question is little different.

I have nested hierarchy of Fragments like in below structure:

<
10条回答
  •  爱一瞬间的悲伤
    2020-12-05 03:35

    public static boolean isFragmentVisible(Fragment fragment) {
        Activity activity = fragment.getActivity();
        View focusedView = fragment.getView().findFocus();
        return activity != null
                && focusedView != null
                && focusedView == activity.getWindow().getDecorView().findFocus();
    }
    

    original post

提交回复
热议问题