IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager
Please check my answer here. Basically I just had to :
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
Don't make the call to super()
on the saveInstanceState
method. This was messing things up...
This is a known bug in the support package.
If you need to save the instance and add something to your outState
Bundle
you can use the following:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
In the end the proper solution was (as seen in the comments) to use :
transaction.commitAllowingStateLoss();
when adding or performing the FragmentTransaction
that was causing the Exception
.
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
You should do the transaction in a Handler
as follows:
@Override
protected void onPostExecute(String result) {
Log.v("MyFragmentActivity", "onFriendAddedAsyncTask/onPostExecute");
new Handler().post(new Runnable() {
public void run() {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.remove(dummyFragment);
ft.commit();
}
});
}
Can not perform this action after onSaveInstanceState - android
Used transaction.commitAllowingStateLoss();
instead of transaction.commit();
If you do this than your final state in not allow saved but it is ok if you don't care
For more clarification about commit()
and commitAllowingStateLoss()
read this blog.
java.lang.IllegalStateException(Can not perform this action after onSaveInstanceState)
As i can not comment on your question due to less reputation point.
I assume this is your public method to change fragment.
public void beginTransaction(ID id, Bundle bundle)
In this method every time you are adding fragment to activity. So if you are adding fragment first time this will work fine but in case of second fragment you should use replace not add
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
I think after doing this you should not face this problem. or you can use 'replace' for both first and second fragment.
I hope this will help you.
Related Topics
Android: How to Get the Current Foreground Activity (From a Service)
How to Detect If Software Keyboard Is Visible on Android Device or Not
How to Prevent Android Taking a Screenshot When My App Goes to the Background
Firebaseinstanceidservice Is Deprecated
Close Application and Launch Home Screen on Android
How to Send Device to Device Notification by Using Fcm Without Using Xmpp or Any Other Script.
Call an Activity Method from a Fragment
Android: Go Back to Previous Activity
Update Data in Listfragment as Part of Viewpager
How to Validate an E-Mail Address
How to Display Count of Notifications in App Launcher Icon
Post Multipart Form Data Using Retrofit 2.0 Including Image
Android - How to Override the "Back" Button So It Doesn't Finish() My Activity
How to Do Something Like a Flowlayout in Android
How to Start a Service When .Apk Is Installed For the First Time