How do you handle a tag that arrives while your activity is open?
Deliver it to the instance already on screen. Declare the activity singleTop, or add FLAG_ACTIVITY_SINGLE_TOP to the foreground dispatch intent, and read the tag in onNewIntent. Without that, each tap starts a new copy of the activity on top of the last one, and Back walks through every earlier tap.
What happens by default
A tag is delivered with startActivity, and the default launch mode answers every start with a new instance. The tasks and back stack guide shows the result: a stack of A, B, C and D becomes A, B, C, D and D.
For a screen that reads tags, that means a new copy per tap. Each copy has its own state and its own tag, and Back walks through all of them before it leaves the app.
singleTop and onNewIntent
With the singleTop launch mode, an intent for an activity already at the top of the stack goes to that instance, through onNewIntent(), and no copy is created. FLAG_ACTIVITY_SINGLE_TOP on the intent has the same effect, which is why the foreground dispatch example in the Advanced NFC guide adds it to its PendingIntent.
The delivery goes through a pause. The Activity reference says an activity never receives a new intent while resumed: it is paused, receives the intent, then resumes. Foreground dispatch, disabled in onPause and enabled again in onResume, follows that cycle on every tap.
singleTask also routes the intent to the existing instance, but it changes the task the activity lives in and destroys the activities above it. For a reading screen, singleTop is the smaller change.
getIntent still holds the first tag
The Activity reference is explicit: after onNewIntent(), getIntent() still returns the original intent. Code that reads the tag from getIntent() in onResume processes the first tag again on every later tap.
Call setIntent() with the new intent, or read the tag in onNewIntent() and never from getIntent().
An old tag is worse than a wrong one. The Tag reference says a new Tag object is created at each discovery and only the most recent can open a technology class, and an object reused after its tag has left raises SecurityException.
Reader mode avoids the question
Reader mode does not use intents. Tags reach a callback in the activity that enabled it, so there is no launch mode to get wrong and no second instance to create.
For a screen that reads many tags in a row, such as a stock check, it is the simpler design, compared on the reader mode page.
Frequently asked questions
Why does my app process the first tag again after a second tap?
getIntent still returns the original intent after onNewIntent. Call setIntent with the new one, or read the tag in onNewIntent.
Should the activity use singleTask instead?
It also delivers to the existing instance, but it changes the activity's task and destroys the activities above it. singleTop is the narrower fix for a reading screen.
Why does Back walk through several copies of my screen?
The activity uses the standard launch mode, so every tap started a new instance. singleTop, or the single top flag on the dispatch intent, stops the stacking.
Is the tag still usable when onNewIntent runs?
Only while it is still in the field. Move any exchange off the main thread straight away, since an exchange started late meets a tag the user has already taken away.