Ritt
Ritt

Reputation: 3349

Issue with ViewPager and FragmentStatePagerAdapter when activity is destroyed

I have an activity which creates viewpager adapter (FragmentStatePagerAdapter) and sets the viewpager to this adapter.

This is how viewpager adapter is created and is set to the viewpager widget.

viewPageAdapter = new viewPageAdapter(getSupportFragmentManager(), getApplicationContext());

mCustomSwipeViewPager.setAdapter(viewPageAdapter);

And this is how i am creating viewPageAdapter

public class viewPagerAdapter extends FragmentStatePagerAdapter {


    private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public viewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // returning newly created fragment
    }

    @Override
    public int getCount() {
        // returning total count
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        // This is where i am putting created fragment in the sparse array,
        // which i will be accessing in the activity based on position
        // for updating fragment views.
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // And here i am removing it from the sparse array
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }
}

Now, i went to the developers options and checked "Donot keep activities".

This is where the problem starts, after moving forward this activity, this activity will get destroyed, which is fine.

Now if i come back, the a new adapter is created in onCreate() and is set to the viewpager, but it's not calling getItem() again, instead it's calling instantiateItem(), with fragment in memory as null.

I checked the fragmentmanager in debug mode, the fragment manager holds two fragments as activestate, with all fragment fields as null.

Things i have tried...

However, if comment out the super.onSavedInstance() of activity, it's working fine, but this is not i want as it's failing for few cases.

Is there any way wherein if i create a new adapter and again set it to the viewpager, it should start afresh, i.e should call getItem() from first position without minimizing the performance of fragmentStateViewPagerAdapter.

Any kind of help or suggestion would be appreciated.

Upvotes: 2

Views: 1357

Answers (2)

Castaldi
Castaldi

Reputation: 701

Had the same issue, was assigning the fragment's view container an id programmatically, we removed this and assigned an id in its layout XML file and the issue went away.

Upvotes: 0

Ritt
Ritt

Reputation: 3349

The only solution i figured out is to comment out the default super.onSaveInstanceState(outState) inside onSaveInstanceState(Bundle outState) callback of activity lifecycle.

@Override
protected void onSaveInstanceState(Bundle outState) {
   //        super.onSaveInstanceState(outState);
}

Doing so will not keep the fragment old instance in the fragmentManager of viewpager and will start afresh.

I couldn't thought of any better solution, as there are many global variables inside fragment and in also in activity and it's presenter, and saving them in the onSaveInstanceState bundle and them restoring them in onCreate or in onRestoreInstanceState() would be very heavy for me.

Any better solution or approach than this is still appreciable.

Upvotes: 3

Related Questions