native.requestExit() No Java State

Hi Brent,

I’ve done some digging and found out which line of code called after “requestExit” is causing the error. Several functions are tied to “applicationExit,” one of which is to schedule a notification for an hour after the user exits the program. The following line is causing the issue:

[lua]local newNotification = notifications.scheduleNotification( getNotificationTime(timeFromNow), notificationOptions )[/lua]

Just to be clear, I have checked the arguments being passed into the “scheduleNotification” function, and they are correct.

@zabace, we’ve confirmed this to be a bug with the Android “notification” plugin during app exit.  Thanks for reporting this issue.

In the meantime, we’ve also confirmed that you can work-around this issue by using our deprecated system.scheduleNotification() API on Android.  That API works without issue during app exit.  But I recommend that you *only* call it on Android like this…

-- Fetch a callback to scheduleNotification() function depending on the platform here. -- Use system.scheduleNotification() on Android. -- Use the notification plugin's scheduleNotification() on all other platforms. local scheduleNotificationCallback if (system.getInfo("platformName") == "Android") then scheduleNotificationCallback = system.scheduleNotification else scheduleNotificationCallback = notifications.scheduleNotification end -- Call the scheduleNotificationCallback like this. local newNotification = scheduleNotificationCallback(getNotificationTime(timeFromNow, notificationOptions)

Thanks Brent and Joshua, I appreciate your attention to this issue. I’m glad I could help identify the cause of this problem.

Do I need to downgrade my Corona version to use “system.scheduleNotification”? I have copied and pasted the code and I am still receiving the same error.

>> Do I need to downgrade my Corona version

No.  The deprecated system.scheduleNotification() API is still available in the current daily builds.  And it still works on Android (I’ve confirmed it with the newest daily build).  You may get a warning message about it being deprecated, but you can safely ignore it.  Just don’t call that function on iOS.

Also note that you cannot safely call *any* of the Android notification plugin’s API upon application exit.  This includes the plugin’s cancelNotification() function.  If you’re calling other notification APIs too, then you’ll need to switch those over to the system API on Android as well.

I’ve confirmed that the system notification API works with the following “main.lua” code.  It’ll generate a schedule notification upon app exit.

-- The notification plugin will crash if you call an API while backing out of the activity. -- It will not crash if you use the deprecated "system" notification API. local notifications = require("plugin.notifications") local function onSystemEvent(event) if (event.type == "applicationExit") then local scheduleNotificationCallback if (system.getInfo("platformName") == "Android") then scheduleNotificationCallback = system.scheduleNotification else scheduleNotificationCallback = notifications.scheduleNotification end scheduleNotificationCallback(2) end end Runtime:addEventListener("system", onSystemEvent) local textSettings = { text = "Press the BACK key to reproduce the notification bug.", x = display.contentCenterX, y = display.contentCenterY, width = display.contentWidth \* 0.9, align = "center", } display.newText(textSettings)

I have just checked and the daily build is aborting the function and causing the app to hang which states that system.cancelNotification has been deprecated, use “plugin.notifications” instead.

The system.cancelNotification() API is still supported on Android.

The trick is to *only* use that API on Android by doing a platform name check as I’ve shown to you up above.

Hi Joshua, I did some deep searching again and I realised that it is not the notification that is causing the problem. However it is the loadsave function that Rob Miracle created. https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

So if I utilized the function from the loadsave, my app will crash when the user hits the back button.

Any advice we can solve this or maybe Rob Miracle can help on this?

Edit: I noticed saving to TemporaryDirectory is okay but calling loading from TemporaryDirectory will still cause the crash.

I looked over the “loadsave.lua” file.  I don’t see anything in that file which calls into Java.  I don’t think this is the cause of this exception.

Also, it’s highly unusual for an Android app to lose its Java state or VM when backing out of an app.  On Android, when you back out an app, it only closes the activity window.  The application process (along with its Java state/vm) is still left alive in the background.  Are you calling os.exit() anywhere in your code?  If so, then I think that’s the real issue because that will force quit your app in a not-so-nice way.  Bad things can definitely happen when using that API.  It’s like Ctrl+Alt+Deleting an app on Windows.  The graceful way to exit an app is via our native.requestExit() function.