onSaveInstanceState () and onRestoreInstanceState ()

前端 未结 13 2339
孤城傲影
孤城傲影 2020-11-22 04:48

I\'m trying to save and restore the state of an Activity using the methods onSaveInstanceState() and onRestoreInstanceState().

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:31

    From the documentation Restore activity UI state using saved instance state it is stated as:

    Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:

    IMO, this is more clear way than checking this at onCreate, and better fits with single responsiblity principle.

提交回复
热议问题