在androidx.fragment.app.Fragment中,不赞成使用setUserVisibleHint(),为什么不执行? [英] in androidx.fragment.app.Fragment,setUserVisibleHint()is Deprecated,and not executed,why?

查看:2393
本文介绍了在androidx.fragment.app.Fragment中,不赞成使用setUserVisibleHint(),为什么不执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (getUserVisibleHint()) {
        isVisible = true;
        onVisible();
    } else {
        isVisible = false;
        onInVisible();
    }
}

我发现这部分代码没有执行.

I found that this part of the code is not executed.

推荐答案

他们只是更改了Fragments中的API.

They just changed API in Fragments.

如果您使用此方法限制片段的生命周期:

If you use this method to limit fragments lifecycle:

您现在可以通过调用设置片段的最大生命周期状态 FragmentTransaction上的setMaxLifecycle().这取代了现在 不推荐使用setUserVisibleHint().

You can now set a max Lifecycle state for a Fragment by calling setMaxLifecycle() on a FragmentTransaction. This replaces the now deprecated setUserVisibleHint().

来源: https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0-alpha07 .

如果需要此方法,因为您尝试检测当前在ViewPager中可见的片段.现在,您可以只使用onResumeonPause方法,但是在此之前,您应该更改FragmentPagerAdapter构造函数中的默认行为.

If you need this method because you try to detect which fragment is currently visible in ViewPager. You can now just use onResume and onPause methods instead but before that you should change default behaviour in FragmentPagerAdapter constructor.

像这样:

FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)

这篇关于在androidx.fragment.app.Fragment中,不赞成使用setUserVisibleHint(),为什么不执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆