永久性修改启动的活动意图 [英] Permanently modify Intent that started an Activity

查看:124
本文介绍了永久性修改启动的活动意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送一个Intent来启动一个活动。我希望能够修改这一意图。然后,当活动被破坏并重新创建,我想这些修改仍然是present当我打电话 getIntent()

目前,修改意图正常工作,只要活动并没有被破坏。如果是,那么当活动被重新创建,它会得到启动它的原意,并没有收到,当它被在第一时间推出可能已经修改的副本。


解决方案

  

修改意图删除我的额外数据工作正常,只要主要活动仍然存在,但如果它破坏/重新创建,额外的数据又回来了。


那是因为你正在修改本地的拷贝意图,而不是主副本保存在一个操作系统的过程,其中的任务列表将被保留。

如果这个数据是名副其实的活动实例状态,应(保存为这样,通过的onSaveInstanceState),和你找回来通过 onRestoreInstanceState()。库的用户需要在这些事件转发到你。

如果您不想考虑这是实例的状态,而是处理状态,将数据存储在一个单例。

如果该数据应该活不过一个进程的生命周期,它写入到磁盘的某个地方。


  

我可以保存在主机应用程序的广播接收器中的数据,然后用在我的code删除


如果按保存在主机应用程序的广播接收器中的数据,这是没有意义的。一个manifest注册接收生活为单播,然后就完成了。

I would like send an Intent to start an Activity. I would like to be able to modify that Intent. Then, when the activity is destroyed and recreated, I would like those modifications to still be present when I call getIntent().

Currently, modifying the intent works fine as long as the Activity has not been destroyed. If it has, then when the activity is recreated, it will get the original Intent that started it, and not the copy it received when it was launched the first time that may have modified.

解决方案

Modifying the Intent to remove my extra data works fine as long as the main Activity is still around, but if it's destroyed/recreated, the extra data is back.

That's because you are modifying your local copy of the Intent, not the master copy maintained in an OS process, where the task lists are kept.

If this data is truly instance state of the activity, it should be saved as such, via onSaveInstanceState(), and you'd get that back via onRestoreInstanceState(). The user of your library would need to forward these events on to you.

If you do not wish to consider this to be instance state, but rather process state, store the data in a singleton.

If the data should live beyond the lifetime of a process, write it to disk somewhere.

I could save the data in the host app's broadcast receiver, and then use and delete it in my code

If by "save the data in the host app's broadcast receiver", that is pointless. A manifest-registered receiver lives for a single broadcast, and then is done.

这篇关于永久性修改启动的活动意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆