美文网首页android开发日常总结Android
Fragment 中setUserVisibleHint方法不执

Fragment 中setUserVisibleHint方法不执

作者: 楷桐 | 来源:发表于2017-06-10 17:59 被阅读5399次
    /**
         * 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

    相关文章

      网友评论

      本文标题:Fragment 中setUserVisibleHint方法不执

      本文链接:https://www.haomeiwen.com/subject/dhkcqxtx.html