判断fragment显示和隐藏,判断fragment是否在前台

addFragment或者replaceFrament

@Override
public void onResume() {
    super.onResume();
    //TODO now visible to user
}
@Override
public void onPause() {
    super.onPause();
    //TODO now invisible to user
}

使用show和hide来显隐的Fragment

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if(hidden){
        //TODO now invisible to user
    } else {
        //TODO now visible to user
    }
}
@Override
public void onPause() {
    super.onPause();
    //TODO now visible to user
}

在ViewPager中的Fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser){
        //TODO now it's visible to user
    } else {
        //TODO now it's invisible to user
    }
}
 @Override
public void onResume() {
    super.onResume();
   //TODO now it's visible to user
}

@Override
public void onPause() {
    super.onPause();
    //TODO now it's invisible to user
}

你可能感兴趣的:(判断fragment显示和隐藏,判断fragment是否在前台)