startActivityForResult 后的活动状态
在android文档中这样说:
...当活动暂停或停止时,活动的状态为 保留。这是真的,因为 Activity 对象仍然保存在 暂停或停止时的内存——有关其成员的所有信息 并且当前状态仍然存在。因此,用户所做的任何更改 保留活动内的内容,以便当活动返回时 前台(当它“恢复”时),这些更改仍然存在。
但是在我的活动中,当我从其他活动返回时,可扩展列表被重置(所有展开的组都被折叠)。数据不会被修改,适配器和游标不会被通知或重新查询。 我检查过 onDestroy 回调方法从未被调用,只有 onPause 和 onStop 。
我使用 startActivityForResult 退出活动,然后使用 finish() 返回。
请问,为什么我会失去状态? 谢谢。
In the android documentantion said this:
...when an activity is paused or stopped, the state of the activity is
retained. This is true because the Activity object is still held in
memory when it is paused or stopped—all information about its members
and current state is still alive. Thus, any changes the user made
within the activity are retained so that when the activity returns to
the foreground (when it "resumes"), those changes are still there.
But in my Activity, when I come back from other activity the expandablelist is reset (all the expanded groups are collapsed). The data is not modified and the adapter and cursor are not notify or requery.
I've checked the onDestroy callback method is never called, only onPause and onStop.
I go out of the activity using startActivityForResult and I come back using finish().
Please, why I am losing the state?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很多时候,当您使用
startActivityForResult
时,您会丢失状态。您需要通过重写onSaveInstanceState
并将数据添加到 Bundle 来保存所有相关数据。还要重写onRestoreInstanceState
来处理您返回并需要恢复数据的情况。因此,在您的情况下,您可以将可扩展列表的哪些部分正在扩展添加到 Bundle 中,以便您在返回时可以再次扩展它们。Many times when you use
startActivityForResult
, you lose your state. You need to save all pertinent data by overriding theonSaveInstanceState
and adding data to the Bundle. Also overrideonRestoreInstanceState
to handle when you come back and need to restore your data. So in your case you might add to the Bundle which parts of the expandablelist are expanding so that you can expand them again when you return.