skip to Main Content

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:

enter image description here

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


  1. Chosen as BEST ANSWER

    Problem solved :-)

    Andrew pointed me in the right direction.

    I had to change launchMode in ...androidappsrcmainAndroidManifest.xml:

    android:launchMode="singleTask"
    

    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


  2. To rely on the basic intent nfc functionality you need to add android:launchMode="singleTop" option to the Activity with the NFC intent filter in the Manifest.

    From the docs

    the "standard" and "singleTop" modes differ from each other in just one respect: Every time there’s a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created

    As standard is the default launchmode 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 the onNewIntent 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search