In the parent activity or fragment where the adapter instance is available:

// In the Activity or wherever you have access to the view pager instance
adapter.fetchFragmentByPosition(mPager, 0);

This should allow you to get access to the instance even when the registeredFragments becomes null on orientation change. Can anyone else report back on if this code a) is working on orientation change b) isn't working on orientation change or c) proposed workaround

Wish I had this code earlier. I came across such a situation, and did not know what to do. Spent a lot of time, and finally made a hack from reading the internal code of FragmentStatePagerAdapter. Below is what I came up with, but your solution is very simple.

//Sparse Array to keep track of the registered fragments in memoryprivateSparseArray<Fragment> mRegisteredFragments =newSparseArray<>();
//Stores a reference to the FragmentManager usedprivateFragmentManager mFragmentManager;
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mFragmentManager = fragmentManager;
}
/** * Overriding to restore the state of Registered Fragments array * * @param state is the Parcelable state * @param loader is the ClassLoader required for restoring the state*/@Overridepublicvoid restoreState(Parcelable state, ClassLoader loader) {
super.restoreState(state, loader);
if (state !=null) {
//When the state is presentBundle bundle = (Bundle) state;
//Setting the ClassLoader passed, onto the Bundle
bundle.setClassLoader(loader);
//Retrieving the keys used in BundleSet<String> keyStringSet = bundle.keySet();
//Iterating over the Keys to find the Fragmentsfor (String keyString : keyStringSet) {
if (keyString.startsWith("f")) {
//Fragment keys starts with 'f' followed by its position indexint position =Integer.parseInt(keyString.substring(1));
//Getting the Fragment from the Bundle using the Key through the FragmentManagerFragment fragment = mFragmentManager.getFragment(bundle, keyString);
if (fragment !=null) {
//If Fragment is valid, then update the Sparse Array of Registered Fragments
mRegisteredFragments.put(position, fragment);
}
}
}
}
}