在androidx.fragment.app.Fragment中,setUserVisibleHint()被Deprecated,并没有执行,为什么? [英] 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:
您现在可以通过调用为 Fragment 设置最大生命周期状态在 FragmentTransaction 上设置 MaxLifecycle().这取代了现在不推荐使用 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()被Deprecated,并没有执行,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!