androidfragmentstatepageradapter

Android FragmentStatePagerAdapter for custom View


because I don't like the default implementation of Android's Bottom Navigation Bar, I have made my own and connected it with a FragmentStatePagerAdapter. I just want to know how I can get already created Fragments with their saved instance state from my adapter just like viewpager does? It would be nice if anyone could help me solve this.


Solution

  • I solved it on my own. I totally had forgotten to care about the Saved Instance State of my view. I got it from the ViewPager source code and modified it slightly because some Methods were marked as deprecated. Here the implementation.

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SaveState ss = new SaveState(superState);
        ss.position = lastSelectedPosition;
        ss.itemId = lastSelectedItemId;
        if (mAdapter != null) {
            ss.adapterState = mAdapter.saveState();
        }
        return ss;
    }
    
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SaveState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SaveState ss = (SaveState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        if (mAdapter != null) {
            mAdapter.restoreState(ss.adapterState, ss.loader);
            setSelectedItem(ss.position, ss.itemId);
            lastSelectedItemId = ss.itemId;
            lastSelectedPosition = ss.position;
        }
    }
    
    public static class SaveState extends BaseSavedState {
        int position;
        int itemId;
        Parcelable adapterState;
        ClassLoader loader;
    
        SaveState(Parcelable superState) {
            super(superState);
        }
    
        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
            out.writeInt(itemId);
            out.writeParcelable(adapterState, flags);
        }
    
        public static final Parcelable.ClassLoaderCreator<SaveState> CREATOR = new 
        Class later dater<SaveState>() {
            @Override
            public SaveState createFromParcel(Parcel source) {
                return new SaveState(source, null);
            }
    
            @Override
            public SaveState[] newArray(int size) {
                return new SaveState[size];
            }
    
            @Override
            public SaveState createFromParcel(Parcel source, ClassLoader loader) {
                return new SaveState(source, loader);
            }
        };
    
        SaveState(Parcel in, ClassLoader loader) {
            super(in);
            if (loader == null) {
                loader = getClass().getClassLoader();
            }
            position = in.readInt();
            itemId = in.readInt();
            adapterState = in.readParcelable(loader);
            this.loader = loader;
        }
    
    }