javaandroidandroid-viewpagerfragmentstatepageradapter

Android FragmentStatePagerAdapter not updating ViewPager correctly when adding/removing pages


I'm trying to create a browser with tabs that you can easily add, remove, and swipe between. Currently I have a ViewPager with each paged fragment containing a WebView and some buttons.

The problem is I can't seem to reliably update the ViewPager once I add or remove tabs. I believe this is partly caused by a bug in the FragmentStatePagerAdapter but the workarounds don't seem to work with my use case or I've been unable to implement them (I'm an Android+Java noob).

Ideally adding and removing tabs won't cause the WebViews in any pages/tabs to be unnecessarily reloaded. At the very least they need to be restored to the state they were before.

Currently, there are two specific problems:

Here is my adapter code:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

import java.util.ArrayList;

public class BrowserTabRepository extends FragmentStatePagerAdapter {

public Browser browser;

public ArrayList<BrowserTabFragment> mFragmentList = new ArrayList<BrowserTabFragment>();
public boolean removing;

public BrowserTabRepository(FragmentManager fm, Browser browser) {
    super(fm);
    this.browser = browser;
}

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

@Override
public BrowserTabFragment getItem(int position) {
    return mFragmentList.get(position);
}

public boolean hasItem(int position) {
    return position < mFragmentList.size();
}

public void clearAllItems() {
    mFragmentList.clear();
    this.notifyDataSetChanged();
}

public void updateItem(int position, BrowserTabFragment fragment) {
    mFragmentList.set(position, fragment);
    notifyDataSetChanged();
}

@Override
public int getItemPosition(Object object) {
    // if(removing)return POSITION_NONE; workaround/hack I found. Tabs don't get messed up but the adapter incorrectly returns the state of the fragment that has just been deleted instead of the one replacing it
    if (mFragmentList.contains(object)) {
        final int index = mFragmentList.indexOf(object);
        return index;
    }
    return POSITION_NONE;
}

public void removeBrowserTab(int position, BrowserTabViewPager viewPager) {
    BrowserTabFragment fragment = mFragmentList.get(position);
    if (fragment != null) {
        removing = true;
        //fragment.isBeingRemoved = true;
        mFragmentList.remove(fragment);
        destroyFragmentView(viewPager, position, fragment);

        notifyDataSetChanged();
        removing = false;
    }
    ;
}

protected void destroyFragmentView(ViewGroup container, int position, Object object) {
    FragmentManager manager = ((Fragment) object).getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.remove((Fragment) object);
    trans.commit();
}

@Override
public int getCount() {
    return mFragmentList.size();
}

public void addBrowserTab(BrowserTabFragment fragment, int positionToAdd) {
    mFragmentList.add(positionToAdd, fragment);
    notifyDataSetChanged();
}

public void addBrowserTab(BrowserTabFragment fragment) {
    mFragmentList.add(fragment);
    notifyDataSetChanged();
}
}

Here is the code to create the adapter+viewpager in my main Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browser);
    browserTabRepository = new BrowserTabRepository(this.getSupportFragmentManager(), this);
    viewPager = (BrowserTabViewPager) findViewById(R.id.viewPagerContainer);
    viewPager.setAdapter(browserTabRepository);
    viewPager.setSaveFromParentEnabled(false);
    viewPager.setOffscreenPageLimit(2); // how many tabs can be held in memory!
}

Here is the possibly relevant life cycle code in the fragment:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    webView.saveState(outState); // save state of web view when it is  paused (in background, rotated etc)
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_browser, container, false);
    webView = (CustomWebView) rootView.findViewById(R.id.activity_main_webview);
    // ... load tab buttons, configure web view and other stuff ...
    if(startingPageAddress!=null){
        goToAddressOrSearchString(startingPageAddress);
        startingPageAddress = null;
    }else if(savedInstanceState!=null) {
        webView.restoreState(savedInstanceState); // restore state of bundle
    }
    return rootView;
}

Solution

  • I eventually gave up with this approach as it's a long standing bug with the FragmentStatePagerAdapter that I was unable to fix 100% for requirement of highly dymamic tabs.

    A much better solution I found was to switch to using a RecyclerView + PagerSnapHelper + RecyclerView.Adapter as this works in pretty much the same way minus the bugs