How to determine when Fragment becomes visible in ViewPager
How to determine when Fragment becomes visible in ViewPager
You can do the following by overriding setUserVisibleHint
in your Fragment
:
public class MyFragment extends Fragment {
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
else {
}
}
}
How to know when fragment actually visible in viewpager
Of course. Assuming that viewPager
is your instance of the ViewPager
, use: viewPager.getCurrentItem()
.
Within your Fragment
you can check if its instance is visible to the user like so:
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible) {
Log.i("Tag", "Reload fragment");
}
}
Always make sure that you search for answers throughly before asking your question. For instance, the first place you should check would be: https://developer.android.com/reference/android/support/v4/view/ViewPager.html
How to check if a Fragment is visible in a ViewPager?
You're passing in 0
as the behavior, which corresponds to the deprecated BEHAVIOR_SET_USER_VISIBLE_HINT
. By using that behavior, setUserVisibleHint()
will be called with true
when your fragment becomes the current page and false
when it is not.
If you switch to the not deprecated BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
, then only the current page will get onResume()
. When the page changes, that Fragment will get a callback to onPause()
and the newly active fragment will get a call to onResume()
.
Detect when fragment becomes visible
EDIT:
As pointed out in the comments, setUserVisbileHint()
does not get called automatically, but by the FragmentPageAdapter when used in a ViewPager.
For the scenario described in the question, onHiddenChanged(boolean hidden)
http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean) is suitable.
As stated in the documentation, the method gets called every time the Fragments hidden state changes, but not on the initial setup of the Fragment.
You could use setUserVisibleHint()
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
if isVisibleToUser
is set to true
the fragment should be displayed.
How to check if the fragment is visible to user
I finally came across the solution while trying myself:
Simple check for isResumed()
and it'd only return true if the fragment is in the foreground and resumed state.
Just to be safe, check for isVisible
which also checks for isAdded
internally and would reduce the condition, so a simple if-condition
:
if (isVisible() && isResumed()) {
// perform your action here
}
This worked for me!
How to make sure current Fragment is visible in ViewPager
While trying setUserVisibleHint()
, When you are using the same Class for all 3 Fragment, You will get called 3 times or 2 times based on the position it will get call setUserVisibleHint()
, but in that one time it will be true and remaining times will be false, because currently, one Fragment will be visible.
In that case Instead of checking only true or false, just check with true with some Id(make sure its null-pointer check, Example if you moving from 2nd position to 3rd, then 1st position arguments which we send through setArguments
will be null).
Example:
I have one Fragment class using for 3 Fragment in my ViewPager
, I want to animate some View
when user come focus to the Fragment.
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
startAnimation(); // If I comment this line my View will be hidden always because I am hiding the View when it's not focused on the user
} else {
checkAndHide();
}
}
public void startAnimation() {
// View might not created, when calling this, because setUserVisibleHint get call before onCreateView()
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mView != null) {
}
}
}, 500);
}
Determine which fragment currently visible in ViewPager
ViewPager.OnPageChangeListener
is the obvious approach here. And as you have noticed, onPageSelected(int position)
doesn't get called until the page is centered (if you are slowly scrolling the page by touch and holding on).
What I actually need: I need to determine which fragment is currently visible in ViewPager, no matter from FragmentActivity or from Fragment itself. How can I achieve this?
First, you haven't defined "currently visible". In a ViewPager
, 2 pages can both be seen while swiping between them, so which one is "currently visible" mid-swipe? My assumption is that you would consider whichever page is "more visible" to be "currently visible".
Having that said, I would look into using onPageScrolled (int position, float positionOffset, int positionOffsetPixels)
. It appears that positionOffset
may be able to give you the information you need. I would test it with some logging statements to see what data returns.
How to know if a Fragment is Visible?
You should be able to do the following:
MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
//DO STUFF
}
else {
//Whatever
}
Related Topics
Sha-1 Fingerprint of Keystore Certificate
Simple Android Grid Example Using Recyclerview With Gridlayoutmanager (Like the Old Gridview)
How to Resize a Bitmap in Android
How to Make a Custom Keyboard in Android
Instant Run in Android Studio 2.0 (How to Turn Off)
Getactivity() Returns Null in Fragment Function
Different Values Folders in Android
How to Make Links in a Textview Clickable
How to Define Dimens.Xml For Every Different Screen Size in Android
Android Accelerometer Accuracy (Inertial Navigation)
Android - Dynamically Add Views into View
Facebook Android Generate Key Hash
How to Save Users Score in Firebase and Retrieve It in Real-Time in Android Studio