What event is triggered when a tab fragment is selected

徘徊边缘 提交于 2019-11-30 11:29:28
Doug Simonton

Try setUserVisibleHint() in the fragment as described in this answer. When the fragment is in the selected tab, setUserVisibleHint() will be called with true, and when the fragment is not the selected tab, setUserVisibleHint() will be called with false. This works for me using the support library.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser)
        Log.d("MyFragment", "Fragment is visible.");
    else
        Log.d("MyFragment", "Fragment is not visible.");
}
Farman Ali Khan

You can override setUserVisibleHint(boolean isVisibleToUser) or onHiddenChanged (boolean hidden) method.

  • In case of setUserVisibleHint(boolean isVisibleToUser),
    isVisibleToUser=true when fragment is visible and isVisibleToUser=false when fragment is hidden.

  • In case of onHiddenChanged (boolean hidden), hidden:True if the
    fragment is now hidden, false if it is not visible.

I haven't tested this, but I believe you can use the onHiddenChanged method of the Fragment

From the docs:

Called when the hidden state (as returned by isHidden() of the fragment has changed.

http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged%28boolean%29

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!