3

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.

Bitbang3r
  • 6,826
  • 5
  • 27
  • 40
  • Removing extras in JB has issues. If you return to the activity after removing the extra, it's still invoked. Any ideas? Tnx! – MSquare Jun 24 '13 at 10:06

1 Answers1

2

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.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • In light of both this answer and MSquare's comment (above), it looks like the safest thing to do is to just leave Intent extras alone and let the garbage collector deal with them. – Bitbang3r Jun 28 '13 at 18:23