Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between commit() and commitAllowingStateLoss() in Fragments

I was using the commit method in my project that built it with fragments.

Anyway, sometimes I was getting IllegalStateException: Can not perform this action after onSaveInstanceState error and I couldn't find any good solution about it but just this method commitAllowingStateLoss(). I changed commit function to commitAllowingStateLoss() but didn't use it long time to test so may this function help me? And the main question, what is the difference between commit() and commitAllowingStateLoss()?

like image 566
Mert Aydin Avatar asked May 25 '13 09:05

Mert Aydin


People also ask

What is the difference between commit and commitAllowingStateLoss in Android?

There is only one difference between commit() and commitAllowingStateLoss() : the latter doesn't throw an exception if state loss occurs. Other than that, they have identical behavior.

What is the difference between commit and commitNow()?

The former only executes the current transaction synchronously, whereas the latter will execute all of the transactions you have committed and are currently pending. commitNow() prevents you from accidentally executing more transactions than you actually want to execute.

What is Android fragment commit?

Commit is asynchronousCalling commit() doesn't perform the transaction immediately. Rather, the transaction is scheduled to run on the main UI thread as soon as it is able to do so. If necessary, however, you can call commitNow() to run the fragment transaction on your UI thread immediately.

What is the use of addToBackStack in Android?

addToBackStack. Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.


2 Answers

There is only one difference between commit() and commitAllowingStateLoss(): the latter doesn't throw an exception if state loss occurs. Other than that, they have identical behavior.

See my blog post about this topic for more information.

like image 82
Alex Lockwood Avatar answered Sep 30 '22 17:09

Alex Lockwood


commit():

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

commitAllowingStateLoss():

A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.

If you do commit() after onSaveInstance(), you will get below exception:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574) 
like image 23
Jitesh Mohite Avatar answered Sep 30 '22 16:09

Jitesh Mohite