Created
December 10, 2015 21:03
-
-
Save akuafif/00f171a659cc97bdaafb to your computer and use it in GitHub Desktop.
[Java/Android] Detect the fragment is on focus (User is viewing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Useful for viewPagers. | |
public class MyFragment extends Fragment { | |
@Override | |
public void setUserVisibleHint(boolean isVisibleToUser) { | |
super.setUserVisibleHint(isVisibleToUser); | |
if (isVisibleToUser) { | |
// User is viewing the fragment, | |
// or fragment is inside the screen | |
doYourThing(); | |
} | |
else { | |
// User is not viewing the fragment, | |
// or fragment is our of the screen | |
doYourThing(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment