Native alerts issue

Hello,

I am having an issue when chaining multiple alerts.

This code, after pressing “Yes” on the retry alert, the simulator crashes, when compiled to android, nothing happens (no crash, no action, nothing, like if the call back was never called, even if I check for clicked or cancelled events)

-- some button triggers this event local buttonPress = function(event) local webCallback = nil --forward declaration local alertId = nil local onCompletePopUp = function(event) if event.action == "clicked" then if event.index == 1 then alertId = native.showAlert("title", "loading..") network.request("some url", webCallback, some\_params) else -- some code end end end webCallback = function(event) native.cancelAlert(alertId) -- actually more code here depending on the response, but this is the part failing. native.showAlert("title", "retry?", {"Yes","No"}, onCompletePopUp) end alertId = native.showAlert("title", "loading..") network.request("some url", webCallback, some\_params) end

Now there is this other code, it works as expected on the simulator. But on android, the callback function “clearData” which is supposed to be called after the user presses ok on the alert, is called right after the “loading” alert dissapears, without user interaction.

So the user is moved to the next scene, with the second alert shown.

local function update(event) local updatingAlert = nil local callback = function(isError) local clearData = function(event) -- some code -- go to some scene. end native.cancelAlert(updatingAlert) if isError then native.showAlert("title", "SECOND ALERT", {"Ok"}, clearData) else native.showAlert("title", "SECOND ALERT", {"Ok"}, clearData) end end updatingAlert = native.showAlert("title", "please wait... FIRST ALERT") some\_function(callback) -- async function with a callback end

Any advice on what is wrong or if this a known bug would be great.

Perhaps using alerts as “loading screens” is a bad idea.

In the second example in clearData you might want to check if “clicked” == event.action then .

I’ve had similar issues with native alerts. If I want to call one immediately after another, I have to use a timer.performWithDelay to trigger the second one.  

I think the reason is that it’s all native, and the OS won’t allow 2 alerts to be on screen at the same time. When one closes, it must take a second to register that it has closed, in the mean time any calls to open a new alert will detect that the previous one is still open, and will fail.

See if this works for you:

local onCompletePopUp = function(event) if event.action == "clicked" then if event.index == 1 then timer.performWithDelay(1000, function() alertId = native.showAlert("title", "loading..") end, 1) network.request("some url", webCallback, some\_params) else -- some code end end end

I’ve had problems triggering all kinds of things from an alert view in the past - changing scenes, IAP…some of these things may be fixed now, I haven’t tried them out. I tend to make my own popup instead now, that way I have more control over it and can theme it to fit my app.

About this, if I do that, then the clear data code is never called on mobile devices.

This is weird, it seems that the event fired on corona simulator is “clicked”, but on android is “cancelled”.

About adding delays, that might be a work around but the the alerts will look ugly… I would have a moment where the “loading” alert faded but the other one didn’t show yet so the user might think everything completed.

Any tips on making your own pop ups?

@alan Is it really necessary to make the delay that long? Maybe just a 10ms or 1ms delay is enought to force it to run in next frame when the previous alert was already canceled. You might want to test it. It will blink though so you could put a shadow rect on screen same as native alert does between these two frames.

1000ms was just to test if it fixes the problem, you could then always adjust it down until you have a time that works for you. As I say, I stopped using native alerts where it caused me problems.

As for advice on making my own popups, I just make them the way I would make any other images and buttons. I just create a group with a background panel, some text and whatever buttons I need, and then return that group whenever I call my popup.

I can confirm delays seem to fix the issue. With 100ms It is working on the emulator, but what about the devices? Maybe if a device is not fast enough 100ms wont be enough… So it might be risky.

Try just 1ms usually in situations like this it is enought to set just 1ms as it forces the function to be called in the next frame which would give OS time to dispatch messages to the native object.

Ya 1ms seem to work. I will change my approach to use less alerts anyways.

In the second example in clearData you might want to check if “clicked” == event.action then .

I’ve had similar issues with native alerts. If I want to call one immediately after another, I have to use a timer.performWithDelay to trigger the second one.  

I think the reason is that it’s all native, and the OS won’t allow 2 alerts to be on screen at the same time. When one closes, it must take a second to register that it has closed, in the mean time any calls to open a new alert will detect that the previous one is still open, and will fail.

See if this works for you:

local onCompletePopUp = function(event) if event.action == "clicked" then if event.index == 1 then timer.performWithDelay(1000, function() alertId = native.showAlert("title", "loading..") end, 1) network.request("some url", webCallback, some\_params) else -- some code end end end

I’ve had problems triggering all kinds of things from an alert view in the past - changing scenes, IAP…some of these things may be fixed now, I haven’t tried them out. I tend to make my own popup instead now, that way I have more control over it and can theme it to fit my app.

About this, if I do that, then the clear data code is never called on mobile devices.

This is weird, it seems that the event fired on corona simulator is “clicked”, but on android is “cancelled”.

About adding delays, that might be a work around but the the alerts will look ugly… I would have a moment where the “loading” alert faded but the other one didn’t show yet so the user might think everything completed.

Any tips on making your own pop ups?

@alan Is it really necessary to make the delay that long? Maybe just a 10ms or 1ms delay is enought to force it to run in next frame when the previous alert was already canceled. You might want to test it. It will blink though so you could put a shadow rect on screen same as native alert does between these two frames.

1000ms was just to test if it fixes the problem, you could then always adjust it down until you have a time that works for you. As I say, I stopped using native alerts where it caused me problems.

As for advice on making my own popups, I just make them the way I would make any other images and buttons. I just create a group with a background panel, some text and whatever buttons I need, and then return that group whenever I call my popup.

I can confirm delays seem to fix the issue. With 100ms It is working on the emulator, but what about the devices? Maybe if a device is not fast enough 100ms wont be enough… So it might be risky.

Try just 1ms usually in situations like this it is enought to set just 1ms as it forces the function to be called in the next frame which would give OS time to dispatch messages to the native object.

Ya 1ms seem to work. I will change my approach to use less alerts anyways.