question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

You must disable foreground dispatching while your activity is still resumed

See original GitHub issue

Description

Had anyone seen this ? “You must disable foreground dispatching while your activity is still resumed”

On Android - I need nfc scanning to listen for the tag only when button is pressed. iOS - works ok for below code.

Steps to Reproduce

  1. Android app, when listening for tag one after another.
  2. below is the code from view model
	private async Task ScanAPointAsync()
        {
            IsBusy = true;
            try
            {
                string[] options = new string[] { "Scan Barcode" };
                if (CrossNFC.IsSupported && CrossNFC.Current.IsAvailable)
                {
                    options = new string[] { "Scan Barcode", "Read a Tag" };
                }

                var result = await page.DisplayActionSheet("Select", "Cancel", null, options);
                if (result == "Read a Tag")
                {
                    if (CrossNFC.Current.IsEnabled)
                    {
                        CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived;
                        CrossNFC.Current.StartListening();
                    }
                    else
                    {
                        await page.DisplayAlert("Enable NFC", "Please enable NFC on your phone.", "OK");
                    }
                }
            }
            catch (Exception)
            {
                await page.DisplayAlert("Error", "Something went wrong, Please try again.", "OK");
            }
            finally
            {
                IsBusy = false;
            }
        }

	private async void Current_OnMessageReceived(ITagInfo tagInfo)
        {
            if (tagInfo != null)
            {
		string serialNumber = tagInfo.SerialNumber;
		
		// do something more here

                CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
                CrossNFC.Current.StopListening();
            }
        }

Expected Behavior

shouldnt crash

Actual Behavior

App Crashes with “You must disable foreground dispatching while your activity is still resumed”

Basic Information

  • Version with issue: 0.1.16
  • Last known good version: NA
  • IDE: Visual Studio
  • Platform Target Frameworks:
    • Android: 10.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kberawalacommented, May 21, 2020

You probably need to unsubscribe and stop listening in the main thread like this:

Device.BeginInvokeOnMainThread(() =>
{
    CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
    CrossNFC.Current.StopListening();
});

It worked. Thanks

2reactions
franckbourcommented, May 21, 2020

You probably need to unsubscribe and stop listening in the main thread like this:

Device.BeginInvokeOnMainThread(() =>
{
    CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
    CrossNFC.Current.StopListening();
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

NFC disable/enable foreground dispatching at will
This method must be called from the main thread, and only when the activity is in the foreground (resumed). Also, activities must call ......
Read more >
Android – NFC disable/enable foreground dispatching at will
Foreground dispatch can only be enabled when your activity is resumed. Best Solution. I believe you should be using disableForegroundDispatch(Activity) in order ...
Read more >
NfcAdapter.DisableForegroundDispatch(Activity) Method
Disable foreground dispatch to the given activity.
Read more >
Advanced NFC overview
This document describes advanced NFC topics, such as working with various tag technologies, writing to NFC tags, and foreground dispatching, ...
Read more >
core/java/android/nfc/NfcAdapter.java
throw new IllegalStateException("You must disable forgeground dispatching " +. "while your activity is still resumed");. } } catch (RemoteException e) {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found