在onDestroy之后活动不恢复数据

时间:2016-01-10 12:53:01

标签: java android android-activity onrestoreinstancestate

我遇到两个ActionBarActivities的问题。 我有第一个Activity(WorkoutEditActivity),它从WorkoutEditActivity内的Fragment开始另一个Activity(A2)和startActivityForResult。 当我从A2按下ActionBar上的后退按钮时,我的第一个Activity被Destroyed,并将数据保存在onSaveInstanceState中,然后Activity重新启动,但我无法弄清楚如何恢复数据。 以下是不起作用的相关代码:

8151-8151/localhost.fitnessoptimizer D/STOP﹕ stopping act
01-10 13:26:45.110    8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ onDestroy called
01-10 13:26:45.110    8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying Luffy
01-10 13:26:45.110    8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying -1
01-10 13:26:45.110    8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ was destroyed
01-10 13:26:45.118    8151-8151/localhost.fitnessoptimizer D/onCreate﹕ called
01-10 13:26:45.118    8151-8151/localhost.fitnessoptimizer V/Monotype﹕ SetAppTypeFace- try to flip, app = localhost.fitnessoptimizer
01-10 13:26:45.118    8151-8151/localhost.fitnessoptimizer V/Monotype﹕ Typeface getFontPathFlipFont - systemFont = default#default
01-10 13:26:45.126    8151-8151/localhost.fitnessoptimizer W/ViewPager﹕ Requested offscreen page limit 0 too small; defaulting to 1
01-10 13:26:45.126    8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? null
01-10 13:26:45.126    8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? 1
01-10 13:26:45.127    8151-8151/localhost.fitnessoptimizer D/START﹕ starting act
01-10 13:26:45.129    8151-8151/localhost.fitnessoptimizer D/RESUME﹕ resuming act
01-10 13:26:45.129    8151-8151/localhost.fitnessoptimizer D/RESUME﹕ extras did not contain workout2
01-10 13:26:45.129    8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? null
01-10 13:26:45.129    8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? 1
01-10 13:26:45.152    8151-8151/localhost.fitnessoptimizer D/GOT WORKOUT﹕ null

以下是我的日志摘录

{{1}}

onRestoreInstanceState永远不会被调用,onCreates savedInstanceState也不包含这些信息。 如何保存或恢复锻炼(以及whatShouldBeSaved,实际上只是一个调试变量)?

PS:我把我的代码放在片段js标签中,因为它已经很长了

2 个答案:

答案 0 :(得分:0)

来自官方网站onSaveInstanceState

  

被调用以在被杀死之前从活动中检索每个实例状态,以便可以在onCreate(Bundle)或onRestoreInstanceState(Bundle)中恢复状态(此方法填充的Bundle将被传递给两者)。

当您在活动中按BACK时,它将被关闭,但不会被杀死。 “杀戮”可以由Android系统完成,当应用程序处于后台时,系统需要暂时释放一些资源。

您需要改为使用Shared Preferences

答案 1 :(得分:0)

onSaveInstanceState()仅在您的活动意外销毁时才会被调用,并且当活动被当前销毁时不会被调用。与 onRestoreInstanceState()相同 官方网站Recreating an Activity