How to know when fragment actually visible in viewpager
Asked Answered
S

5

12

I am using 4 fragments inside a ViewPager ,as ViewPager load the previous and next fragment in advance ,and no lifecycle method is called when navigating between fragments. So is there any way to detect when Fragment is actually visible. Thanks in Advance.

Sami answered 20/2, 2018 at 19:41 Comment(1)
Please provide a minimal, complete, and verifiable example of what you have tried. stackoverflow.com/help/mcveFoxhole
G
5

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

Grigson answered 20/2, 2018 at 19:43 Comment(4)
Thanks Matt .Actually I want to update the some view when Fragment is visible ,so just looking for any callback method which is called every time when fragments is visible to userSami
Well you can create your own callback and, for example, call a method of the viewPager from within setUserVisibleHint of each one of your Fragment instances. If you want to update the same view, then this method itself is your answer. Just replace Log.i() with the code that you want to run each time the fragment becomes visible.Grigson
Be very careful: "this method may be called outside of the fragment lifecycle"Ibidem
its only work with FragmentPagerAdapter !!! its not goodLupien
F
22

as per @Matt's answer setUserVisibleHint is deprecated

so here is alternative way for this.

    @Override
    public void setMenuVisibility(boolean isvisible) {
        super.setMenuVisibility(isvisible);
        if (isvisible){
            Log.d("Viewpager", "fragment is visible ");
        }else {
            Log.d("Viewpager", "fragment is not visible ");
        }
    }
Frighten answered 26/8, 2020 at 5:26 Comment(5)
@JohnGlen which issue you have been face?Frighten
I thought if the Fragment was visible, that would mean it was rendered on screen. That was my misunderstanding.Feather
@JohnGlen okay then you need to revert your downvoteFrighten
I tried, but it won't let me till the answer is editied.Feather
It is not working for FragmentBiphenyl
G
5

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

Grigson answered 20/2, 2018 at 19:43 Comment(4)
Thanks Matt .Actually I want to update the some view when Fragment is visible ,so just looking for any callback method which is called every time when fragments is visible to userSami
Well you can create your own callback and, for example, call a method of the viewPager from within setUserVisibleHint of each one of your Fragment instances. If you want to update the same view, then this method itself is your answer. Just replace Log.i() with the code that you want to run each time the fragment becomes visible.Grigson
Be very careful: "this method may be called outside of the fragment lifecycle"Ibidem
its only work with FragmentPagerAdapter !!! its not goodLupien
D
2

You can use viewPager.getCurrrentItem() to get the currently selected index, and from that you should be able to extrapolate which fragment is shown. However what you probably want is to use addOnPageChangeListener() to add an OnPageChangeListener. This will let you keep track of what page is selected, as it's selected by implementing the onPageSelected(int selected) method.

Declared answered 20/2, 2018 at 19:46 Comment(0)
H
2

Did you try the isVisible method in the fragment?

Hackney answered 20/2, 2018 at 19:49 Comment(2)
ViewPaget2 isVisible is helping to avoid additional calls when it is not a current fragment.Dorey
This does not work if you need to do stuff as soon as a Fragment is visible.Feather
B
0

Nowadays you can override androidx.fragment.app.onResume and androidx.fragment.app.onPauseto detect if it is visible or not respectively.

Beller answered 3/11, 2022 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.