Sky Kelsey Sky Kelsey - 8 months ago 67
Android Question

Permanently modify Intent that started an Activity

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


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.