I want to launch my Flutter Android App automatically, after an Ndef Tag has been discovered. I’m using an Intent filter to achieve this.
All works fine, apart that when launched via the Ndef Tag, the App has an Nfc App Icon.
Also, if the app was already opened manually before, it will have the App open twice:
a) The manually opened one with the correct logo
b) The Ndef Tag launched one
Screenshot with two instances of the app:
To exclude any side effects, I did a new Flutter project from scratch and only did the following changes in …androidappsrcmainAndroidManifest.xml:
Added this right after the <manifest> tag:
<uses-permission android:name="android.permission.NFC"/>
And after the existing
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
I added this filter to launch the app:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" android:host="developer.android.com" android:pathPrefix="/test" />
</intent-filter>
The Nfc Tag only has one record of type Uri, with the content https://developer.android.com/test
How can I avoid the app clone, and just always open the same App instance with the correct App logo?
2
Answers
Problem solved :-)
Andrew pointed me in the right direction.
I had to change
launchMode
in ...androidappsrcmainAndroidManifest.xml:Andrew's link to the docs above helped me getting started.
And this link gives the overall pig picture, how Android tasks and activities work: link
To rely on the basic intent nfc functionality you need to add
android:launchMode="singleTop"
option to theActivity
with the NFC intent filter in the Manifest.From the docs
As
standard
is the defaultlaunchmode
then a new instance of the Activity is created when the NFC is scanned every time, hence multiple instances.With
singletop
it will re-use one that is at the top of the stack BUT you will have to handle the processing the data in theonNewIntent
method as well as where you are processing it now.Using the method does have the downside that your App with be paused and resumed to receive the NFC data, so your App has to pause and resume correctly. It is generally better to use one of the Foreground NFC methods like enableReaderMode instead of the basic intent methods, for Flutter there are various flutter packages to do this.