You must disable foreground dispatching while your activity is still resumed
See original GitHub issueDescription
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
- Android app, when listening for tag one after another.
- 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:
- Created 3 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It worked. Thanks
You probably need to unsubscribe and stop listening in the main thread like this: