Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Android's NFC foreground dispatch system have a bug?
    primarykey
    data
    text
    <p>I have an annoying issue with the foreground dispatch behavior. Sometimes instead of calling <code>onNewIntent()</code>, it completely recreates the activity, which breaks the app's workflow. </p> <p><strong>My concrete situation</strong>: Activity A is the MainActivity, which uses the foreground dispatch. Everything works as it should. However, in my activity B, which is launched from the browser (VIEW action), the foreground dispatch doesn't work under some circumstances anymore. </p> <p><strong>The workflow:</strong></p> <ul> <li><p>I start the MainActivity, switch to the browser (without closing the MainActivity), launch activity B and attach my NFC device --> it creates a new activity B.</p></li> <li><p>I start the MainActivity and close it again. After that I switch<br> to the browser, launch activity B and attach my NFC device --> everything works with <code>onNewIntent()</code></p></li> </ul> <p>The code is correct, e.g. if I attach the NFC device in the first scenario twice, it works as it should at the second time, but not at the first time. In the MainActivity and activity B I definitively call the disableForegroundDispatch() method in the activity's onPause() method.</p> <p>Is there a solution for my specific problem? For me it sounds like a bug.</p> <p><strong>Edit:</strong> </p> <pre><code>public void resume(Activity targetActivity) { if (nfc != null &amp;&amp; nfc.isEnabled()) { // nfc is the default NFC adapter and never null on my devices Intent intent = new Intent(targetActivity, targetActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(targetActivity, 0, intent, 0); nfc.enableForegroundDispatch(targetActivity, pendingIntent, null, new String[][] { new String[] { IsoDep.class.getName() } }); } } public void pause(Activity targetActivity) { if (nfc != null &amp;&amp; nfc.isEnabled()) { nfc.disableForegroundDispatch(targetActivity); } } </code></pre> <p>These methods are called in the corresponding methods in each activity. Thanks for the help!</p> <p><strong>Solution:</strong> After a very long research I finally found the issue. Logcat printed: </p> <ul> <li>startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent</li> </ul> <p>I found other issues at Stackoverflow, where people had have the same issue with the NotificationManager, but all the hints didn't help me. Adding the flag singleTask to my activity B did the trick for me, but to be honest I don't understand it, because the context is always an activity. </p> <p>I removed all the code from the MainActivity and the first scenario still didn't work. I romved the MainActivity from the manifest and after that everything was fine. Maybe it is a problem, that an app instance is running and activity B is launched from the browser? I don't know. </p> <p>Anyway, thanks for the help NFC guy!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload