So I've run into the infamous Can not perform this action after onSaveInstanceState problem and am having a tough time figuring out how to fix it.
Currently I can cause it by logging out of my application via the onNavigationItemSelected
menu.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FirebaseAuth.getInstance().signOut();
goToLogin();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void goToLogin() {
Intent x = new Intent(MainActivity.this, LoginActivity.class);
startActivity(x);
finish();
}
Although when I log back into the application
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(intent, REQUEST_SIGNUP);
finish();
Then attempt to click a recycler view item
CompletedViewPagerFragment completedViewPagerFragment = new CompletedViewPagerFragment();
Bundle bundle = new Bundle();
bundle.putInt(CompletedViewPagerFragment.KEY_COMPLETED_SELECTED, index);
completedViewPagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeholder, completedViewPagerFragment, VIEWPAGER_FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I receive the onSaveInstanceState error
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1832)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1850)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:643)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:603)
at com.example.jack.mhealth.MainActivity.onRecordingSelected(MainActivity.java:295)
I've attempted to use the .commitAllowingStateLoss() although that gives me a activity has been destroyed error
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1854)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:643)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:608)
at com.example.jack.mhealth.MainActivity.onRecordingSelected(MainActivity.java:294)
I'm really lost of what has gone wrong between logging out and finishing the MainActivity to then logging back in and causing that ViewPager to break.
When the app is fresh it it goes straight to the login screen and then logs in from MainActivity:75 it works as expected when logging in. DO I need to reset the view pager somehow?
I was going to delete this question due to finding the mistake but thought I'd post my solution just incase someone else runs into this problem.
My mistake was I was using a Singleton class to hold the view pager adapter
(I cannot remember exactly why I was using a singleton class but it was that which was causing the errors.)