Application Receiving Nfc Always Pops Up New Instance In Front
Solution 1:
You receive the NFC intent (action TAG_DISCOVERED
) because you registered for it in the manifest:
<intent-filter><actionandroid:name="android.nfc.action.TAG_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter>
This is also the reason why your activity is recreated upon receiving the intent.
The intent filter that you register for the foreground dispatch (action NDEF_DISCOVERED
with any MIME type) does not seem to match your tag (or you did not yet invoke the piece of code that would enable the foreground dispatch).
Note that calling disableForegroundDispatch()
will ony disable a foreground dispatch previously registered through enableForegroundDispatch()
. It will not influence the intent filters in your manifest. See Android: Can I enable/disable an activity's intent filter programmatically? on how you could selectively disable intent filters registered in the manifest. However, with regard to NFC intents, you would probably want to register to receive events for all tags through the foreground dispatch system and then, upon receiving the event in onNewIntent()
selectively ignore tags that you don't want.
A few more things about your code (actually only about the NFC parts)
For the
NDEF_DISCOVERED
intent filter in your manifest you would typically also want to specify a<data ... />
element that matches the data type of your tags.Do not use a
TAG_DISCOVERED
intent filter in your manifest (unless you really understand and want the impact of this). TheTAG_DISCOVERED
intent filter (when used in the manifest) is merely a compatibility mode for API level 9 (before Android 2.3.3) where NFC support was very, very limited and a fall-back mode that can be used to create apps that handle NFC tags that are not supported by any other app.The
TECH_DISCOVERED
intent filter requires a tech-list XML file in order to match any tag. Thus, your current version of this filter in your manifest will never match anything.Calling
disableForegroundDispatch()
inonResume()
does not make any sense. By design, the foreground dispatch could never be enabled at this point of the activity life-cycle. The reason for this is that you must not callenableForegroundDispatch()
beforeonResume()
and you are required to calldisableForegroundDispatch()
at latest inonPause()
.In fact it does not make sense to use
enableForegroundDispatch()
/disableForegroundDispatch()
anywhere other than inonResume()
/onPause()
. If you want to stop listening for tags upon other events (e.g. a pressing a button), you would simply remember your current state (process/don't process NFC events) in some flag and, when you receive a new NFC intent inonNewIntent()
, you would either process or silently ignore the tag based n that flag.Your code should not manually call activity life-cycle methods (as you currently do with
onNewIntent(getIntent());
).
Post a Comment for "Application Receiving Nfc Always Pops Up New Instance In Front"