Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Do something when Fragment is visible
public class BaseFragment extends Fragment {
private boolean fragmentResume=false;
private boolean fragmentVisible=false;
private boolean fragmentOnCreated=false;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (!fragmentResume && fragmentVisible){ //only when first time fragment is created
doWorkOnVisible();
}
}
@Override
public void setUserVisibleHint(boolean visible){
super.setUserVisibleHint(visible);
if (visible && isResumed()){ // only at fragment screen is resumed
fragmentResume=true;
fragmentVisible=false;
fragmentOnCreated=true;
doWorkOnVisible();
}else if (visible){ // only at fragment onCreated
fragmentResume=false;
fragmentVisible=true;
fragmentOnCreated=true;
}
else if(!visible && fragmentOnCreated){// only when you go out of fragment screen
fragmentVisible=false;
fragmentResume=false;
}
}
// Override this in derived Fragment classes
protected void doWorkOnVisible(){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment