native.requestExit() No Java State

Hi,

I have tried to implement back button on Android to exit when the application is at the starting scene. So by clicking back button will eventually exit the app. However, I am getting an error popping “Unfortunately, your app has stopped” instead of closing peacefully.

I did a logcat and see the following.

I/Corona (20494): Runtime error I/Corona (20494): no Java state I/Corona (20494): stack traceback: I/Corona (20494): [C]: in function 'request' I/Corona (20494): ?: in function '?' I/Corona (20494): ?: in function 'func' I/Corona (20494): /Users/jenkins/slaveroot/workspace/Templates/label/andro id/platform/resources/init.lua:241: in function \</Users/jenkins/slaveroot/worksp ace/Templates/label/android/platform/resources/init.lua:221\>

What I did was to start with an eventListener for the back button

Runtime:addEventListener( "key", onKeyBlankEvent ) 

Followed by calling the request exit

local function onKeyBlankEvent(event) local phase = event.phase local keyName = event.keyName if ( "back" == keyName and phase == "up" ) then native.requestExit() return true else return false end end

Anything I did wrong here to get the error? I even tried sending the app to another scene with just native.requestExit() but upon exit, I still get the same error. I’m on build 2015.2646

Thanks in advanced.

I, too, am receiving this error. My build is 2015.2722

Hello,

Can you try triggering the .requestExit() following a very short timer of, say, 10-50 milliseconds?

Brent

I have tried a delay of 200 milliseconds, and I still get the same error.

Just to be specific about how and where I am getting this error: I have a function that handles all presses of the back button. On the main menu of my app, I show a native alert asking if the user would like to exit the app upon back button press. If the user selects “Yes” then I fire the requestExit function.

Any update on why this might be happening?

Hi @zabace,

We can’t reproduce this “no Java state” error. We modified our sample project “Hardware > KeyEvent” to call “native.requestExit()” when the “back” key has been released (“up” phase), and it gracefully exits the app just fine.

Basically, something else is causing this issue. From looking at the Lua stack trace, the error occurs when an anonymous Lua listener in your code gets called, which is likely happening after the “native.requestExit()” has been called. We would need a simple sample project that can reproduce this issue to help track it down, but before you attempt to create such a project, I encourage you to carefully check aspects of your code which may be causing what I describe herein.

Best regards,

Brent

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.

I, too, am receiving this error. My build is 2015.2722

Hello,

Can you try triggering the .requestExit() following a very short timer of, say, 10-50 milliseconds?

Brent

I have tried a delay of 200 milliseconds, and I still get the same error.

Just to be specific about how and where I am getting this error: I have a function that handles all presses of the back button. On the main menu of my app, I show a native alert asking if the user would like to exit the app upon back button press. If the user selects “Yes” then I fire the requestExit function.

Any update on why this might be happening?

Hi @zabace,

We can’t reproduce this “no Java state” error. We modified our sample project “Hardware > KeyEvent” to call “native.requestExit()” when the “back” key has been released (“up” phase), and it gracefully exits the app just fine.

Basically, something else is causing this issue. From looking at the Lua stack trace, the error occurs when an anonymous Lua listener in your code gets called, which is likely happening after the “native.requestExit()” has been called. We would need a simple sample project that can reproduce this issue to help track it down, but before you attempt to create such a project, I encourage you to carefully check aspects of your code which may be causing what I describe herein.

Best regards,

Brent