commitAllowingStateLoss() and commit() fragment

前端 未结 5 953
陌清茗
陌清茗 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:17

    Just check whether activity is finishing or no, and then commit() the transaction.

    if (!isFinishing()) {
        // commit transaction
    }
    

    The exception you get is in a scenario, when activity is being finished, and you are trying to commit a new transaction, which obviously won't be saved by FragmentManager because the onSavedInstanceState() has already been executed. That's why framework forces you to implement it "correctly".

提交回复
热议问题