i trying to make app with nfc function. the problem is when nfc tag discovered, pending intent always make a new activity that already exist. i\'m using tab host. how to mak
you can use the flag "PendingIntent.FLAG_UPDATE_CURRENT" while calling the "getActivity" method. I2m using it without a problem.
I had a similar problem but not using a TabHost - every time an NFC tag was scanned my app was launching a new activity rather than firing onNewIntent
as I wanted. I was trying to set android:launchMode="singleTask"
in the manifest and use NfcAdapter.enableForegroundDispatch()
in my activities onResume
method. Instead, I abandoned using PendingIntent and set up intent filters my activity in the manifest as follows:
<activity
...
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data android:mimeType="application/vnd.myname.myapp" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
use this flag instead..
new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
Create your pending intent as below:
PendingIntent.getActivity(this, 0,new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);
Put android:launchMode="singleTask"
for your activity (or activities) in the manifest. That does the trick. Whenever an NFC intent is dispatched by the system, always a new Activity will be created. This is unique for NFC intents. So setting android:launchMode="singleTop"
will not work, nor will setting flags in the PendingIntent.
Another solution is to use NfcAdapter.enableForegroundDispatch() in all your Activities. That way your app gets to handle all NFC intents itself directly (via onNewIntent()
).
Try this:
mNotificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
And in your manifest:
android:launchMode="singleTask"