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.
Activityisn't in resumed/paused state until the corresponding method returns, so that might be the cause of the exception(s). Regarding the call toenableForegroundDispatch(), try to post aRunnableto the main thread withinBaseActivity'sonResume().EnterPinActivitywhich was being called withstartActivityForResult(...). When the result was received, my calling activity immediately launched a new activity (withstartActivity()) 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.onResume()from my own code. So it was down twice.