When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

前端 未结 5 1376
梦如初夏
梦如初夏 2020-11-28 20:14

The following figure (from the official doc) describes the well-known lifecycle of an Android activity:

5条回答
  •  渐次进展
    2020-11-28 20:36

    In addition to already posted answers, there is a subtle change introduced in Android P, which is:

    void onSaveInstanceState(Bundle outState)

    If called, this method will occur AFTER onStop() for applications targeting platforms starting with P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

    Source: docs

    As to why this change is introduced, here's the answer:

    ... so an application may safely perform fragment transactions in onStop() and will be able to save persistent state later.

    Source: docs

提交回复
热议问题