7

IllegalStateException: Foreground dispatch can only be enabled when your activity is resumed

and

IllegalStateException: You must disable foreground dispatching while your activity is still resumed

I am getting these exceptions when trying to start/stop NFC foreground dispatch in my BaseActivity.

All my activities extend BaseActivity, but I only get these exceptions after my third activity is shown.

Here are the stacktraces and output from the Android Monitor window:

10-24 12:53:48.802 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:53:48.822 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:53:48.962 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:53:49.322 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:53:49.322 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:53:49.672 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:53:49.672 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:53:49.692 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.502 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:15.502 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:15.512 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.562 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:15.562 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.692 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:15.692 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:15.702 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.612 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:41.622 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:41.622 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:41.682 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:41.682 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.802 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:41.802 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:41.812 28454-28454/com.example E/BaseActivity: Error starting foreground dispatch
  java.lang.IllegalStateException: Foreground dispatch can only be enabled when your activity is resumed
      at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1421)
      at com.nxp.nfclib.NxpNfcLib.startForeGroundDispatch(:4065)
      at com.example.BaseActivity.onResume(BaseActivity.java:422)
      at com.example.HomeActivity.onResume(HomeActivity.java:36)
      at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
      at android.app.Activity.performResume(Activity.java:6495)
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3516)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3558)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1526)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:145)
      at android.app.ActivityThread.main(ActivityThread.java:6145)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-24 12:54:41.832 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:41.832 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:41.832 28454-28454/com.example E/BaseActivity: Error stopping foreground dispatch
  java.lang.IllegalStateException: You must disable foreground dispatching while your activity is still resumed
      at android.nfc.NfcAdapter.disableForegroundDispatchInternal(NfcAdapter.java:1468)
      at android.nfc.NfcAdapter.disableForegroundDispatch(NfcAdapter.java:1454)
      at com.nxp.nfclib.NxpNfcLib.stopForeGroundDispatch(:5082)
      at com.example.BaseActivity.onPause(BaseActivity.java:477)
      at android.app.Activity.performPause(Activity.java:6530)
      at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1308)
      at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3860)
      at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3833)
      at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3808)
      at android.app.ActivityThread.access$1100(ActivityThread.java:181)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:145)
      at android.app.ActivityThread.main(ActivityThread.java:6145)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:41.942 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:41.942 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:42.002 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:42.002 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:42.012 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:56:45.922 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:56:45.922 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:56:45.932 28454-28454/com.example D/BaseActivity: ...done!

And here are the onResume and onPause methods from my BaseActivity:

@Override
protected void onResume() {

    Log.d(LOG_TAG, "onResume()");
    super.onResume();

    try {
        Log.d(LOG_TAG, "startForeGroundDispatch...");
        nxpNfcLib.startForeGroundDispatch();
        Log.d(LOG_TAG, "...done!");
    }
    catch(Exception e) {
        Log.e(LOG_TAG, "Error starting foreground dispatch", e);
        showToast("Error starting foreground dispatch: " + e);
    }

}

@Override
protected void onPause() {

    Log.d(LOG_TAG, "onPause()");
    super.onPause();

    try {
        Log.d(LOG_TAG, "stopForeGroundDispatch...");
        nxpNfcLib.stopForeGroundDispatch();
        Log.d(LOG_TAG, "...done!");
    }
    catch(Exception e) {
        Log.e(LOG_TAG, "Error stopping foreground dispatch", e);
        showToast("Error stopping foreground dispatch: " + e);
    }

}

I'm using the NXP/Taplinx library (for which I don't have the source code) but from viewing the above stacktraces and the Android NfcAdapter documentation for disableForegroundDispatch and enableForegroundDispatch, the code seems to be in order, so I'm not really sure what's really going wrong or why.

Can anyone shed any more light on what the errors are and how to solve?

NB - I've tried moving the super.onResume() and super.onPause() calls to the end of my corresponding BaseActivity method - but it makes no difference.

I've also tried moving my onResume() code into a new onPostResume() method, but that doesn't help either.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • AFAIK, an `Activity` isn't in resumed/paused state until the corresponding method returns, so that might be the cause of the exception(s). Regarding the call to `enableForegroundDispatch()`, try to post a `Runnable` to the main thread within `BaseActivity`'s `onResume()`. – Onik Oct 24 '16 at 12:54
  • Like this? http://stackoverflow.com/a/11125271/1617737 – ban-geoengineering Oct 24 '16 at 13:05
  • 2
    It's OK, I've found the issue now. I have an `EnterPinActivity` which was being called with `startActivityForResult(...)`. When the result was received, my calling activity immediately launched a new activity (with `startActivity()`) and so I think this was causing the NXP/Taplinx library to presume the calling activity was still in the resumed state when it no longer was. I've resolved this now by moving all my NFC stuff out of BaseActivity into a superclass of BaseActivity that my NFC activities now extend instead. Thanks for your help. – ban-geoengineering Oct 24 '16 at 13:40
  • @ban-geoengineering I am having the exact same problem with Taplinx, and I have a similar object hierarchy (NFCActivity with NFC logic extends BaseActivity with no NFC logic). I have 4 activities invoked as this: A -> B -> C -> D, where A&D extend NFCActivity and B&C extend BaseActivity. Could you further explain your solution? Maybe post an answer post with the classes. – OneEyeQuestion Dec 09 '16 at 17:06
  • This happened to me when I was calling `onResume()` from my own code. So it was down twice. – Yetti99 Aug 26 '19 at 14:56
  • This happens when I click back away from the app and goes back into it. – Paula Kristin Sep 03 '19 at 12:58

0 Answers0