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.
Android Fatal Error - Can not perform this action after onSaveInstanceState
This happens because onRequestPermissionresult
is called before activity's onResume
. You can simply fix this by following:
private boolean mShareEmail = false;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mShareEmail = true;
}
}
}
@Override
public void onResume() {
super.onResume();
if (mShareEmail) {
emailShareImpl();
mShareEmail = false;
}
}
Can not perform this action after onSaveInstanceState WHEN commit
Complete solution at Solution for IllegalStateException
Overriding onSaveInstanceSate is a hack which doesnt necessarily work for all the scenerios. Also using commitAllowingStateLoss() is dangerous and could lead to UI irregularities.
We need to understand that IllegalStateException is encountered when we try to commit a fragment after the Activity state is lost - Activity is not in foreground (to understand more about Activity states read this). Therefore to avoid (resolve) this exception we just delay our fragment transaction until the state is restored
Declare two private boolean variables
public class MainActivity extends AppCompatActivity {
//Boolean variable to mark if the transaction is safe
private boolean isTransactionSafe;
//Boolean variable to mark if there is any transaction pending
private boolean isTransactionPending;
Now in onPostResume() and onPause we set and unset our boolean variable isTransactionSafe. Idea is to mark trasnsaction safe only when the activity is in foreground so there is no chance of stateloss.
/*
onPostResume is called only when the activity's state is completely restored. In this we will
set our boolean variable to true. Indicating that transaction is safe now
*/
public void onPostResume(){
super.onPostResume();
isTransactionSafe=true;
}
/*
onPause is called just before the activity moves to background and also before onSaveInstanceState. In this
we will mark the transaction as unsafe
*/
public void onPause(){
super.onPause();
isTransactionSafe=false;
}
private void commitFragment(){
if(isTransactionSafe) {
MyFragment myFragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame, myFragment);
fragmentTransaction.commit();
}
}
What we have done so far will save from IllegalStateException but our transactions will be lost if they are done after the activity moves to background, kind of like commitAllowStateloss(). To help with that we have isTransactionPending boolean variable
public void onPostResume(){
super.onPostResume();
isTransactionSafe=true;
/* Here after the activity is restored we check if there is any transaction pending from
the last restoration
*/
if (isTransactionPending) {
commitFragment();
}
}
private void commitFragment(){
if(isTransactionSafe) {
MyFragment myFragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame, myFragment);
fragmentTransaction.commit();
isTransactionPending=false;
}else {
/*
If any transaction is not done because the activity is in background. We set the
isTransactionPending variable to true so that we can pick this up when we come back to
foreground
*/
isTransactionPending=true;
}
}
Related Topics
Hide/Show Bottomnavigationview on Scroll
Android Textwatcher.Aftertextchanged VS Textwatcher.Ontextchanged
Gradle - Library Duplicates in Dependencies
How to Get an Event in Android Spinner When the Current Selected Item Is Selected Again
Referencing a String in a String Array Resource with Xml
Better Way to Format Currency Input Edittext
Android: Speech Recognition Without Using Google Server
Getexternalstoragepublicdirectory Deprecated in Android Q
Apply a Theme to an Activity in Android
How to Make an App's Background Image Repeat
How to Handle Handler Messages When Activity/Fragment Is Paused
Unexpected Value from Nativegetenabledtags: 0
Error Message 'Java.Net.Socketexception: Socket Failed: Eacces (Permission Denied)'
Android Set Up Volley to Use from Cache
Android: Long Click on a Button -> Perform Actions