What event is triggered when a tab fragment is selected

前端 未结 3 619
孤城傲影
孤城傲影 2021-01-01 18:40

I\'m using tab fragments in an activity and the actionbar hosts the tabs. What I want to do is that whenever a fragment appears (or re-appears) in the view (selected by the

3条回答
  •  没有蜡笔的小新
    2021-01-01 19:15

    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.");
    }
    

提交回复
热议问题