Getting Exception "Illegalstateexception: Can Not Perform This Action After Onsaveinstancestate"

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



Leave a reply



Submit