Question

I have a requirement wherein my application deals with NFC.

I have declared nfc in the manifest file.

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true"/>

Now, my question is, Is it possible to disable the nfc tag detection in a specific activity.

E.g., I have 10 activities and I don't want the 5th Activity to detect nfc. Is it possible? I need suggestions for the same.

Was it helpful?

Solution

You cannot really stop the device from detecting NFC tags. What you can do, however, is to set your activity to receive (and then ignore) all NFC discovery events.

To do this, you would register your activity with the NFC foreground dispatch system using the NfcAdapter's enableForegroundDispatch() method. This gives your current activity precedence in receiving NFC events over all other intent filters. See Advanced NFC: Using the NFC Foreground Dispatch System on how to do this registration.

Once you registered for the foreground dispatch system, your activity will receive all NFC intents in its onNewIntent() method (or as a pending intent result depending on how you register). There, you can simply ignore these events.

OTHER TIPS

You just call simply this one when your activity is destroy or pause

 nfcAdapter.disableForegroundDispatch(getActivity());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top