onSaveInstanceState () and onRestoreInstanceState ()

前端 未结 13 2340
孤城傲影
孤城傲影 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:49

    I found that onSaveInstanceState is always called when another Activity comes to the foreground. And so is onStop.

    However, onRestoreInstanceState was called only when onCreate and onStart were also called. And, onCreate and onStart were NOT always called.

    So it seems like Android doesn't always delete the state information even if the Activity moves to the background. However, it calls the lifecycle methods to save state just to be safe. Thus, if the state is not deleted, then Android doesn't call the lifecycle methods to restore state as they are not needed.

    Figure 2 describes this.

提交回复
热议问题