How to handle network calls inside a Fragment

前端 未结 7 1112
攒了一身酷
攒了一身酷 2021-01-31 23:21

I have the following situation:

I have an Activity that hosts a ViewPager, and I have 4 Fragments;

the ViewPager

7条回答
  •  野性不改
    2021-01-31 23:39

    Try this, in each fragment override below method and call your function when it is visible:

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisible()){
            if(isVisibleToUser){
                Log.d("MyTag","My Fragment is visible");
            }else{
                Log.d("MyTag","My Fragment is not visible");
            }
        }
    }
    

    EDIT

    Note: This is only useful when using a FragmentPagerAdapter or FragmentStatePagerAdapter

提交回复
热议问题