4

First, the use case : 2 phones have my app opened on the same screen. I want one user to be able to share the screen content (data) with the other one without necessarily opening a new instance of the activity when beaming using NFC. (both Android devices are running Ice Cream Sandwich)

So I have a singleTop activity declared like this in the manifest.

<activity android:name=".activity.MyActivity" android:configChanges="orientation|keyboardHidden" android:launchMode="singleTop">
  <intent-filter android:label="@string/activityLabel">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="a.b.c/x.y.z" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="application/x.y.z"/>
  </intent-filter>
</activity>

When a VIEW action is fired and the activity is already on top, the onNewIntent() method is called in the same instance if the activity.

When a NDEF_DISCOVERED action is fired and the activity is already on top, the onCreate() method is called in a new instance of the activity.

mmathieum
  • 506
  • 5
  • 14

4 Answers4

4

You describe the case that the app is already open and the proper Activity is in the foreground. In that case, you can make use of the foreground-dispatching of NFC intents by calling NfcAdapter.enableForegroundDispatch() in your Activity's onResume() (and disableForegroudDispatch() in onPause()). This will force all NFC Intents to be delivered to your Activity (via onNewIntent()).

ozbek
  • 20,955
  • 5
  • 61
  • 84
NFC guy
  • 10,151
  • 3
  • 27
  • 58
  • This looks like the way to go but I don't have a 2nd NFC-capable device to test it out now. Doc: http://developer.android.com/guide/topics/nfc/advanced-nfc.html#foreground-dispatch – mmathieum Feb 22 '12 at 00:23
  • 3
    Using an NFC tag with an NDEF message would give virtually the same result as receiving data via Android Beam. Perhaps you can try that. – NFC guy Feb 23 '12 at 00:23
0

You should use: android:launchMode="singleInstance" Works for me.

Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
  • launchMode "singleInstance" is only for home screen replacements. In my case "singleTask" worked perfectly. – Androbin Aug 03 '16 at 14:26
0

I don't have an answer for you. But I have a workaround: have the NDEF_DISCOVERED start a new activity. Make that activity invisible (Theme.NoBackground) and in the onCreate, make it start the MyActivity with singleTop and finish immediately. MyActivity should now appear with onNewIntent.

Thierry Roy
  • 8,452
  • 10
  • 60
  • 84
0

Have you looked at the Android Beam sample:

http://developer.android.com/resources/samples/AndroidBeamDemo/index.html

It implements this behavior that you want (minus VIEW intent filtering). I'm not sure if that intent will affect the NDEF_DISCOVERED one, but it would be interesting to maybe modify the Android Beam sample to see if you can cause the same behavior as your app.

robertly
  • 2,132
  • 14
  • 9
  • I looked at the Android Beam sample (started by this actually) and it doesn't implement the foreground dispatching that I'm trying to achieve here. – mmathieum Feb 23 '12 at 05:21