How can I make sure Android Beam is not starting a new instance of my "singleTop" activity?

Firstly, the use case: 2 phones opened my application on the same screen. I want one user to be able to share screen contents (data) with another, without necessarily opening a new instance of activity when shining using NFC. (Ice Cream Sandwich runs on both Android devices)

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="abc/xyz" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="application/xyz"/> </intent-filter> </activity> 

When the VIEW action is executed and the activity is already on top, the onNewIntent() method is called on the instance if it is the action.

When the NDEF_DISCOVERED action NDEF_DISCOVERED started and the action is already enabled, the onCreate() method is called on the new instance.

+4
source share
4 answers

You describe that the application is already open, and the corresponding activity is in the foreground. In this case, you can use the front NFC sending scheme by calling NfcAdapter.enableForegroundDispatch() in your onResume() event (and disableForegroudDispatch() in onPause() ). This will cause all NFC intentions to be delivered to your activity (via onNewIntent() ).

+4
source

I have no answer for you. But I have a workaround: NDEF_DISCOVERED starts a new action. Make this activity invisible ( Theme.NoBackground ) and in onCreate, launch its MyActivity with singleTop and exit immediately. MyActivity should appear with onNewIntent .

0
source

You looked at the sample Android Beam:

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

It implements this behavior that you want (minus VIEW filtering). I'm not sure if this intention will affect NDEF_DISCOVERED, but it would be interesting to possibly modify the Android Beam pattern to see if you can trigger the same behavior as your application.

0
source

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

0
source

Source: https://habr.com/ru/post/1397296/


All Articles