Skip to content Skip to sidebar Skip to footer

How To Intercept Nfc Tag Before Onnewintent Executes

I have an app that captures NFC tags. The problem i have had in the past is that users hover over the tag in an unsteady manner causeing the NFC adapter to trigger twice. I have do

Solution 1:

You seem to insist on not handling NFC intents in onNewIntent(). I would suggest that onCreate() and onNewIntent() both call a common procedure for scanning the tag. In that way, both entry points would follow the same code path.

Your claim that the app "jumps out of onCreate" is probably just a figure of speech? What happens is that your tag scanning AsyncNfcConnect runs on a separate thread as a background task (as it should). That task is created in onCreate() and continues running after onCreate() has finished (you could add add a Log statement at the end of onCreate() to check). When the connection with the tag is lost somehow and the tag is rediscovered, onNewIntent() is called, as you observed.

In any case there is no way to prevent this from happening, so your app has to be able to handle it. To detect this and deal with it, you could set some flag inside your activity to indicate that your background task is running or has run. You could also store the information whether an exception has occurred and simply try scanning the tag again when it is rediscovered (this probably requires that you implement the suggestion I made above). If you want to make your app even more failure proof, you could also store the ID of the last scanned tag to positively identify it again when it is rediscovered after you have successfully scanned it (or not). When exceptions keep occurring with the same tag, you could indicate his after a certain number of times to the user (e.g. by suggesting that the device be positioned differently w.r.t. the tag).

Post a Comment for "How To Intercept Nfc Tag Before Onnewintent Executes"