key back event and native.showAlert

I found what I think is a bug. (Android build 2011.611 running on a HTC thunderbolt)

When a native.showAlert is displayed and the user hits the back button on the device the alert goes away(as it should) but if you call native.showAlert again, it fails to be displayed.

In some cases, you can work around the issue by checking for a dialog first and canceling the dialog(that isn’t displayed) before invoking a new one.

(I tried to work around this by capturing the back event but the back event generated when the dialog is displayed is NOT captured by a key event listener…so you never know if the dialog has been canceled) [import]uid: 40100 topic_id: 15424 reply_id: 315424[/import]

Sounds like a bug to me. Thanks for reporting it. We’ll add this to our to-do list.

In the meantime, I think your only work-around is to cancel the last displayed alert window yourself before displaying the next alert window like shown below. Even though the window is no longer displayed, the code thinks it still is. So canceling it in Lua might be the only way to reset the state.
Note: I haven’t tried this for myself yet, so hopefully this will work.

local alertId = 0  
function ShowMessage(title, message, buttons, listener)  
 if (alertId ~= 0) then  
 native.cancelAlert(alertId)  
 end  
 alertId = native.showAlert(title, message, buttons, listener)  
end  
  
ShowMessage("My Title", "My message.", { "OK" }, nil)  

Please see the following API docs on how to close an alert window in Lua…
http://developer.anscamobile.com/reference/index/nativecancelalert
[import]uid: 32256 topic_id: 15424 reply_id: 57036[/import]

Note: I haven’t tried this for myself yet, so hopefully this will work.

I have tested this workaround and it does work. (for most use scenarios, I have used 8-10 alerts in my app.)

The only place this fails to work is if you are relying on the event handler to perform a critical process. (I had used one at the end of a game level to report the score, etc… the handler started the next level when the dialog was dismissed, if the back button is used, the alert disappears and leaves the game in an unusable state…working on rewriting that bit now.)

[import]uid: 40100 topic_id: 15424 reply_id: 57129[/import]

Hi Allen,

I think this is a high priority issue, so I’m going to fix it today.

I plan on setting up the “Back” button to be regarded as a “Cancel” event in the alert’s Lua listener. That means the Lua listener’s “event.action” property will be set to “Cancel” since no button was pressed on the alert box. Will that work for you?
[import]uid: 32256 topic_id: 15424 reply_id: 57177[/import]

Well… yes, that would work quite nicely.

Thank you… and thank you for being Quick. [import]uid: 40100 topic_id: 15424 reply_id: 57179[/import]

Done! The fix will be made available in the next daily build. [import]uid: 32256 topic_id: 15424 reply_id: 57187[/import]