在androidx.fragment.app.Fragment中,不赞成使用setUserVisibleHint(),为什么不执行? [英] in androidx.fragment.app.Fragment,setUserVisibleHint()is Deprecated,and not executed,why?
问题描述
@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
中可见的片段.现在,您可以只使用onResume
和onPause
方法,但是在此之前,您应该更改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屋!