commitAllowingStateLoss() and commit() fragment

前端 未结 5 955
陌清茗
陌清茗 2021-02-04 03:47

I want commit a fragment after network background operation. I was calling commit() after successful network operation but in case activity goes to pause or stop state it was cr

5条回答
  •  一生所求
    2021-02-04 04:05

    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.

    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.

    commitAllowingStateLoss

    Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

    And According to your question you are using AsyncTask for network background operation. So this might be a problem that you are commiting fragment inside its callback method. To avoid this exception just dont commit inside asynctask callbacks methods. This is not a solution but precaution.

提交回复
热议问题