Question

I am developing an application that is NFC-aware. Today I have an intent-filter in my manifest for handling NFC "bumps" that have NDEF records:

<intent-filter>
  <action android:name="android.nfc.action.NDEF_DISCOVERED" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="wifi" />
  <data android:scheme="socket" />
</intent-filter>

This works just fine.

The app has a "select an item" activity and a "view" activity. As currently written, the Intent is handled in the context of my "select an item" activity. When the app is in the "view" activity, however, I would like the "view" to handle any NFC Intents instead of the "select an item" activity. When the "view" activity is no longer on the stack, I'd like any NFC "bumps" to again be handled by the "select an item" activity.

What is the right/best way to have this happen? Note too that I would not like to see the "which app/activity" selector pop up in this case, since that would make the experience particularly tedious and confusing.

Was it helpful?

Solution

While your activity (either "view" or "select an item") is in the foreground, you can use the foreground dispatch to detect new tags. See the NfcAdapter's enableForegroundDispatch() method. An activity that is registered with the foreground dispatch will take precedence over intent filter's in the app manifests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top