问题
This might be a stupid question, but is there a rule that states that intent extras have to be explicitly removed by the consuming activity, or is that only true if you're recycling Intent objects?
Put another way, if I always chain to the next activity by doing something like:
Intent i = new Intent(MyCurrentActivity.this, FooActivity.class);
i.putExtra("first", "stringvalue");
i.putExtra("second", 69L);
startActivity(i);
then, in FooActivity, I read them back out...
String first = getIntent().getStringExtra("first");
long second = getIntent().getLongExtra("second");
... do I have to also explicitly remove them to avoid accidentally polluting a future activity's intent, or from the moment I finish grabbing them, can I just forget they even exist and move on?
I could swear I remember reading something that said I had to remove them, but I haven't been able to find it again, and I'm suspecting that it might only apply to reused intent objects.
回答1:
If you're planning to use the same Intent object but do not need (nor want) the extras, then you may remove them. If, instead, you want to call the start the same intent with the same extras, then keep them. Lastly, if the object is going to be destroyed, who cares about the extras?
In any case, I would decide on the caller activity, not on the receiver of the intent.
来源:https://stackoverflow.com/questions/6100570/do-intent-extras-have-to-be-removed