Fragment in ViewPager using FragmentPagerAdapter is blank the second time it is viewed
Asked Answered
A

15

103

I have a fragment interface with tabs along the bottom which open different fragments in the main view.

I have one particular fragment which is a list of items. If the user selects one of the items in this list, another fragment opens which contains a viewpager which scrolls horizontally between all of the items in the list in the previous fragment. This works great.

The viewpager uses a FragmentPagerAdapter to display the items.

The problem comes when the user selects an item in the list, views it, then hits the button on the tab bar to go back to the list, then selects another item. The second time an item is selected, a blank screen appears instead of the viewpager. I receive no errors in my LogCat when this happens.

Why is the viewpager only appearing the first time?

FragmentPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    Cursor mCursor;

    public ViewPagerAdapter(FragmentManager fm, Cursor c) {
        super(fm);
        mCursor = c;
    }

    public void changeCursor(Cursor c) {
        mCursor = c;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCursor == null) return 0;
        else return mCursor.getCount();
    }

    @Override
    public Fragment getItem(int position) {
        mCursor.moveToPosition(position);
        return TeamCardFragment.newInstance(mCursor, position);
    }
}

PagerFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);

    View mView = inflater.inflate(R.layout.team_card_master, container, false);
    mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);

    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();

    return mView;
}

private class setAdapterTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCursorPosition);
    }
}
Acetylide answered 12/10, 2011 at 21:11 Comment(0)
A
1

We got around this by re-implementing the view pager items as standard views rather than fragments and changing the adapter accordingly.

Acetylide answered 29/11, 2011 at 19:24 Comment(5)
you can try removing the isViewFromObject(View view, Object object) functionCensorship
There's gotta be a better way.... :/ although the more I dig into Android the less mature I realize it is.Aeschylus
Nested fragments are not supported, so re-implementing with standard view seems the good way to go.Ajmer
I'm struggling with similar issue. I tried using PagerAdapter instead of FragmentAdapter still I encounter the same issue. Please help me out !Catechism
If you want to use nested fragments you can use the adapter I provided in this answer : #7700726Owlet
H
223

I had the same issue. Changing the parent class of my PageAdapter from android.support.v4.app.FragmentPagerAdapter to android.support.v4.app.FragmentStatePagerAdapter solve my ViewPager display issue on "second time"!

Hamm answered 12/12, 2011 at 17:1 Comment(11)
No kidding? I don't have this code anymore otherwise I would test it. Perhaps this answer will help somebody else.Acetylide
What a man! I was on the lookout for this solution for days! Thanks a ton man, works like a charm. But the developer in me is asking what impact does that change make? I mean why didn't it work before and why in StatePagerAdapterKauffmann
also you need to @Override public int getItemPosition (Object object) { return POSITION_NONE; }Defeatist
Worked for me too. Here is a link. It seems to save the fragment state, which makes all the difference. developer.android.com/reference/android/support/v4/app/…Mcguinness
finding it hard to see any situation where you would use fragmentpageradapter instead of fragmentstatepageradapter, is fragmentpageradapter more efficient?Warhorse
This solution doesn't require FragmentActivtiy so you can continue to use native Fragments from API 13! Should be accepted answerExistent
or android.support.v13.app.FragmentStatePagerAdapter for android.app.FragmentManager and not android.support.v4.app.FragmentManagerCrowboot
I see that FragmentStatePagerAdapter makes onCreate() to be called on the fragment before onCreateView(), while FragmentPagerAdapter just calls onCreateView() on the second show of the fragment.Fungoid
@espa_network if you have a pager is inside a fragment, then it probably won't work, because nested fragments are not supported yetChalet
It's a coincidence this works, the right answer is @Daniel F's one. Anyway, for convenience of the commenters here, the difference between FragmentPagerAdapter and FragmentStatePagerAdapter is in the former being more suited for few pages, it retains all of the fragments in memory thus having best performances on page switch, while the latter is more suited for a lot of pages, it destroys fragments, keeping only their state, it's more memory efficient but a little slower on page switch.Formally
Wow... Spent hours on this. So strange. Thank you!!Zelmazelten
E
90

I managed to solve this by replacing getFragmentManager() with getChildFragmentManager() in the parent fragment. This parent fragment was instancing an android.support.v4.app.FragmentPagerAdapter in order to contain pageable (slideable) fragments, which requires a fragment manager in the constructor. To this constructor I passed the return value of getChildFragmentManager().

hackbod's link was key (https://developer.android.com/about/versions/android-4.2.html#NestedFragments), which was found in this post Fragments within Fragments

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions.

Edwin answered 27/3, 2014 at 3:16 Comment(2)
Thankyou... i was having a similar issue trying to re-instantiate a sliding tabs view after onresume() that contained a fragmentpageradapter... the tutorial i used was from 2016 and i still needed to change it to fragmentstatepageradapter to get it to work. wish I knew WHY.Camp
this answer saved lot of my time. Because only using FragmentStatePagerAdapter doesnot solve the question asked here. along with FragmentStatePagerAdapter in parent fragment YourAdapter adapter = new YourAdapter (getChildFragmentManager()); has to be addedLesson
H
18

for me i had to call this on my viewpager:

myViewPager.setSaveFromParentEnabled(false);

I had the issue where the viewpager was not refreshing and all i saw was a blank white screen where the fragments should be. I was passing in getChildFragmentManager but it did not help.

Hoshi answered 12/7, 2017 at 4:11 Comment(11)
Why in the world did this work? This fixed my issue but i have no clue why.Czarina
I removed it from my code now. The fragment manager was causing the issue. Try toggling between getchildfragmentmanager and getfragmentmanagerHoshi
I have already tried that. I have nested Tabs each with ViewPagers that have disabled swiping. I know this is poor design but its the clients wishes not mine. Im using a childFragmentManager for the top level tabbar. Toggling for the lower level tabbar/viewpager didnt seem to fix the issue. I suppose I will stick with the setSaveFromParentEnabled(false) for now since it is working.Czarina
Are you using the latest support libraryHoshi
I was facing the same issue from last 2 days.. Your solution worked for me. thanks !Missus
Hi @Hoshi Your solution worked for me but i don't know why getchildfragmentmanager did not worked for me and i was facing issue only in case when i go from left drawer fragment. But I think it is cauasing little bit performance issueLemuellemuela
myViewPager.setSaveFromParentEnabled(false) worked for me, for the case of Android N 'recents' screen (when holding on square button to open miltwindow frames). Previously ViewPager with FragmentStatePagerAdapter turned blank.Prudi
you guys need to stop using this and fix start using getchildfragmentmanager that should resolve this issue in my opinion. i gave this answer at the time i was not using getchildfragmentmanager. after swapping it correctly this fix was not needed. but maybe every situation is different. visit Yashaswi N P answer first and if that does not work then fall back to mine.Hoshi
@Hoshi I tried other solutions but only yours worked. But is this solution good for performance ? I'm not sure.Cyndy
your not using getChildFragmentManager correctly is my first guess @MehmetGürHoshi
Yo. where how and why did this work!!!. This answer needs upvote! +1 from meBeiderbecke
W
11

In my very particular case, where I was using a CoordinatorLayout with an AppBarLayout and the ViewPager, what solved it for me was removing the android:fitsSystemWindows="true" from my AppBarLayout xml properties.

Don't ask me why. I know it sounds a little bit ridiculous and like it should have no correlation, but it was this single line the only thing that was causing trouble as I was already using getChildFragmentManager() in my adapter. I spent a whole day debugging my code just to find this, so I hope it saves someone else some time.

Wafer answered 22/7, 2016 at 0:36 Comment(3)
Oh man... I was almost gonna surrender with this issue. You made my freaking day! Geez Google, WTF is wrong with the AppBarLayout? I've looked almost hundred pages and spent a ton of hours debugging trying to solve this issue without luck. I'm really grateful that i finally managed to solve the issue with your comment. Thanks a lot! PS: I was so desperate that I even created an issue in the AOSP: code.google.com/p/android/issues/… XDInvertase
Thanks for the hint. At least it has helped us in resolving issue. Now we are trying to find out option for making it full screenNesbitt
You are a lifesaver! I was absolutely clueless why I was having this issue and still clueless why it worked!Jus
C
10

I had the same issue for which I changed adapter from FragmentPagerAdapter to FragmentStatePagerAdapter and getFragmentManager() in the parent fragment to getChildFragmentManager()

The reason behind this is FragmentStatePagerAdapter is helpful in storing a large number of pages and memory associated with each page visited is less as it keeps only the saved state of the fragment while the page is not visible. This reduces the overhead while switching between the fragments.

Combe answered 16/8, 2017 at 16:33 Comment(0)
U
4

Try to put setOffscreenPageLimit (int limit) on parent ViewPager.

mViewPager.setOffscreenPageLimit(totalPages-1);

This worked for me like charm.

In my case I had fragment inside TabLayout with ViewPager.

And another ViewPager inside that fragment. First time all working fine but when I change tab and return back, some of my fragment gone blank.

Unsociable answered 4/10, 2018 at 7:0 Comment(3)
Good answer. It is helped me.Martian
The problem is then it retains all view in memory, which defeats the point of recycling views. OK if you have small amount of tabs.Firsthand
@JeffPadgett , Yes. It's workaround. not solution.Unsociable
B
3

This would help you.

    viewPager.setOffscreenPageLimit(position);
Bulgarian answered 21/8, 2018 at 7:27 Comment(0)
A
1

We got around this by re-implementing the view pager items as standard views rather than fragments and changing the adapter accordingly.

Acetylide answered 29/11, 2011 at 19:24 Comment(5)
you can try removing the isViewFromObject(View view, Object object) functionCensorship
There's gotta be a better way.... :/ although the more I dig into Android the less mature I realize it is.Aeschylus
Nested fragments are not supported, so re-implementing with standard view seems the good way to go.Ajmer
I'm struggling with similar issue. I tried using PagerAdapter instead of FragmentAdapter still I encounter the same issue. Please help me out !Catechism
If you want to use nested fragments you can use the adapter I provided in this answer : #7700726Owlet
A
1

I was getting this same problem on Xamarin Android with a blank screen the second time around.. Setting the following fixed it for me.

viewPager.SaveFromParentEnabled = false;
Alit answered 3/9, 2017 at 7:3 Comment(0)
F
1

class ViewPagerAdapter extends FragmentStatePagerAdapter this work for me try this

Finegrain answered 6/5, 2020 at 5:34 Comment(0)
A
1

simply call getChildFragmentManager() in adapter .Then you will not get blank in multiple click

Akeyla answered 3/12, 2020 at 9:2 Comment(0)
K
0

You can also initialize the adapter for cases where you experience this error when your app is minimized and later called up.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);
    View mView = inflater.inflate(R.layout.team_card_master, container, false);
    mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);
    initViewPagerAdapter();
    return mView;
}
private void initViewPagerAdapter(){
    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();
}

@Override
public void onResume(){
    super.onResume();
    initViewPagerAdapter();
}
Kyte answered 24/7, 2016 at 16:38 Comment(0)
L
0

I had the same thing, in the second time i call the Pager Adapter, the child view return a NullPointerException. And changing the adapder in FragmentStatePagerAdapter solve my issue too.

Lightproof answered 4/8, 2017 at 2:0 Comment(0)
C
0

Change FragmentPagerAdapter into FragmentStatePagerAdapter Use getChildFragmentManager() instead of getFragmentManager()

getChildFragmentManager() - because according to documentation, it will return a private FragmentManager for placing and managing Fragments inside of this Fragment. Meanwhile getFragmentManager() will return the FragmentManager for interacting with fragments associated with this activity

Conlan answered 23/10, 2018 at 12:2 Comment(0)
C
0

I had a similar issue using ViewPager2 and FragmentStateAdapter. When replacing the Fragment (containing a viewpager2 and tabs of fragments) I realized the onDestroyView() method was being called after the new fragment was set up. I fixed this by calling FragmentTransaction.popBackStack() before replacing the fragment, which seemed to clean up the old fragment before creating the new one.

This issue started for me after updating androidx.appcompat:appcompat beyond version 1.2. My Fragment also used to use setRetainInstance(false) which is now deprecated, I'm unsure if that was part of the issue.

Contemn answered 2/11, 2022 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.