简体   繁体   中英

Should I restore savedinstancestate in onCreate or in onRestoreInstanceState?

I have an activity that starts some other activities for results, so when the result comes back, the activity may or may not have been destroyed and recreated.

I have overridden onSaveInstanceState so as to add the data that needs to be preserved and restored.

When the activity gets destroyed and recreated, onCreate is passed the savedInstanceState bundle; but also onRestoreInstanceState() is called and passed the same bundle.

So where should I put the code that extracts the data from the bundle and restores the state? In onCreate or in onRestoreInstanceState? Is the latter guaranteed to be always called?

Is it possible that onRestoreInstanceState is called without calling onCreate? (eg if the activity gets stopped and restarted but not destroyed and recreated)?

"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"

following link explain pretty clearly about restart activity.

Android Guide

Android在onStart()和onResume()生命周期方法之间调用onRestoreInstanceState()方法。因此,为了恢复活动状态,只需实现onRestoreInstanceState()方法恢复活动状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM