Fragment 中setUserVisibleHint方法不执行

/**
     * Set a hint to the system about whether this fragment's UI is currently visible
     * to the user. This hint defaults to true and is persistent across fragment instance
     * state save and restore.
     *
     * <p>An app may set this to false to indicate that the fragment's UI is
     * scrolled out of visibility or is otherwise not directly visible to the user.
     * This may be used by the system to prioritize operations such as fragment lifecycle updates
     * or loader ordering behavior.</p>
     *
     * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
     *                        false if it is not.
     */
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (!mUserVisibleHint && isVisibleToUser && mState < STARTED
                && mFragmentManager != null && isAdded()) {
            mFragmentManager.performPendingDeferredStart(this);
        }
        mUserVisibleHint = isVisibleToUser;
        mDeferStart = mState < STARTED && !isVisibleToUser;
    }

其中有一句话是这么说的:This may be used by the system to prioritize operations such as fragment lifecycle updates

这个可能被用来在一组有序的fragmen里 ,例如 fragment生命周期的更新。告诉我们这个方法被调用希望在一个pager里,因此 FragmentPagerAdapter 可以使用这个,然后调用这个 setUserVisibleHint() 在fragment里,因此有些人也建议重写 setUserVisibileHint() 来知道当前一个fragment对用户来说是隐藏还是显示,这个方法仅仅工作在FragmentPagerAdapter中,不能被使用在一个普通的activity中。

因此我这个单个fragment 就是不被调用的 ,可以重写 onHiddenChanged() 方法来得知当前 fragment 的状态。

http://www.jianshu.com/p/ae3bf6fb2585

    原文作者:ProcessZ
    原文地址: https://www.jianshu.com/p/c8c0b623fe6f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞