Web Popup Doesn't Close on Back Button Press

I’m not sure if this topic has been addressed before but I had a very urgent question. How can I make it so that a Webpopup will not close if the user clicks the hardware soft key on Android. Here is my code, and I’ve tried returning true and false and neither work. I really need your guys help. Thanks

[lua]local function exitWeb(e)
if(e.phase == “ended”) then
native.cancelWebPopup(popup)
xpic:removeSelf()
web:removeSelf()
transition.to(menubutton, {time=1, x = _W/1.1})
mode = 3
return true
end
return true
end
xpic:addEventListener(“touch”, exitWeb)
transition.to(menubutton, {time=1, x = -50})
popup = native.showWebPopup( 20, 50, 275, 400, s.t.itemURL, {} )[/lua]

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 19788 reply_id: 319788[/import]

Hi Jordan,

The reason we allow the Back button to close the web popup is to be consistent with other web popup dialogs on Android such as the login dialog for Twitter and Facebook. Popups like this are expected to be overlayed on top of your app, in which case pressing the Back button to cancel out would be expected. However, we recognize that web popups can be used as part of the app’s main UI, in which case this Back button behavior is not desired.

That said, you can prevent the Back button from closing the web popup by setting the “autoCancel” property to false as follows…

[lua]native.showWebPopup( “http://www.anscamobile.com”, { autoCancel = false } )[/lua]

I hope this helps! [import]uid: 32256 topic_id: 19788 reply_id: 77144[/import]

You are a life saver! Thank you so much!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 19788 reply_id: 77160[/import]

What if I’m using soft keys for navigation? I said autoCancel = false but it didn’t work. [import]uid: 29181 topic_id: 19788 reply_id: 77163[/import]

I just tried it on a KindleFire which has a virtual onscreen Back button and it is working for me.

What exactly is the problem? Is your app exiting when you press the Back button? If so, then that is to be expected. Since the web popup is set up to ignore the Back button, then the Back button will then attempt to exit our of your app. In which case, the only way to prevent this key from exiting your app is to set up a key listener and override the Back key yourself. [import]uid: 32256 topic_id: 19788 reply_id: 77168[/import]

I have tried everything, and it still doesn’t work. Is it because I’m using an event listener for the soft keys? [import]uid: 29181 topic_id: 19788 reply_id: 77169[/import]

No the button isn’t exiting the app. When I click the back button with the soft key it still exits the webpopup here is my code:
[lua]local function myList3Release(e, s)
–for k,v in pairs(s) do print(k,v) end
–system.openURL( s.t.itemURL )
mode=4
local web = display.newRoundedRect(10,10,300,450,12)
web:setFillColor(40,40,40)
localGroup:insert(web)
local xpic = display.newImage(“x.png”)
xpic.x = 290; xpic.y = 30
xpic.xScale = .1; xpic.yScale = .1
localGroup:insert(xpic)

local function exitWeb(e)
if(e.phase == “ended”) then
native.cancelWebPopup(popup)
xpic:removeSelf()
web:removeSelf()
transition.to(menubutton, {time=1, x = _W/1.1})
mode = 3
return true
end
return true
end
xpic:addEventListener(“touch”, exitWeb)
transition.to(menubutton, {time=1, x = -50})
popup = native.showWebPopup( 20, 50, 275, 400, s.t.itemURL, {autoCancel = false} )

end

[lua]That is the webpopup.

[import]uid: 29181 topic_id: 19788 reply_id: 77171[/import]

Here is the code that allows you to switch between different lists:
[lua]local function onBack(event)

local returnValue = true
local phase = event.phase
local whatKey = event.keyName
if whatKey == “back” and phase == “down” then
if mode == 1 then
Runtime:removeEventListener( “key”, onBack );
director:changeScene(“mainmenu”, crossFade)
mode = 0
elseif mode == 2 then
transition.to(myList2, {time=200, x = -5000})
transition.to(myList, {time=200, x = 0})
myList.setAsTarget()
mode = 1
elseif mode == 3 then
transition.to(myList3, {time=200, x=-5000})
transition.to(myList2, {time=200, x=0})
myList2.setAsTarget()
mode=2
elseif mode == 4 then
native.cancelWebPopup(popup)
xpic:removeSelf()
web:removeSelf()
transition.to(menubutton, {time=1, x = _W/1.1})
mode = 3
elseif mode == 0 then
returnValue = false
end
return returnValue
elseif whatkey == “back” and phase ==“up” and mode == 0 then
return false
end
return true
end

Runtime:addEventListener( “key”, onBack );

[lua] [import]uid: 29181 topic_id: 19788 reply_id: 77174[/import]

Josh,

Here is the download link to the unsigned APK so you can see what I’m trying to do. It’s really hard to explain it in writing. If you go to categories, then pick a topic, then click an item, you will see what the problem is. Thanks!

http://www.box.com/s/ig7oyl373892xx5dt8zn

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 19788 reply_id: 77175[/import]

I don’t see anything wrong with your native.showWebPopup() function call. (I assume your s.t.itemURL provides a valid string.)

Which version of the Corona SDK are you using? The autoCancel feature was added to the release version of the Corona SDK (build #704). That is, this feature is not present in older builds. [import]uid: 32256 topic_id: 19788 reply_id: 77176[/import]

Yes it does provide a valid string. I am actually using build #704. Should I update to a newer version? Allow check out the download link to see the problem.

Thanks so much!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 19788 reply_id: 77177[/import]

I just tried your APK and was able to reproduce your problem. According to “adb logcat”, there is a Lua runtime error somewhere in your code when I press the Back button. That is the likely source of the problem. Below is the error message that I’m getting…

I/Corona (15396): Lua Runtime Error: lua_pcall failed with status: 2, error message is: ?:0: attempt to index global ‘xpic’ (a nil value)

It looks like your xpic object has been removed and is nil when your onBack() function code gets called and falls into the mode 4 part of your if/elseif section. That part of the code is also closing the web popup via native.cancelWebPopup(). I recommend that you add Lua print() statements within your code and use “adb logcat” to isolate this issue… because it looks like its working on our end. [import]uid: 32256 topic_id: 19788 reply_id: 77179[/import]

Ok I actually have never heard of that program before. I will have to check it out.

Really thank you so much for your time trying to debug this issue! I truly appreciate it!

Thanks! [import]uid: 29181 topic_id: 19788 reply_id: 77180[/import]

Just an update, the xpic variables were local variables when they needed to be global. Once I fixed that issue and added the autoCancel, the app is working 100% correct

Thank you so much! You saved me! [import]uid: 29181 topic_id: 19788 reply_id: 77181[/import]

Oh great! I’m glad it’s working.

By the way, “adb” is a command line utility that is included with Google’s Android SDK. It allows you to install an app to your device connected to your PC via “adb install”. You can also use it to view a real-time log via “adb logcat”, which is a good way to debug Android apps (the old school way that is). All of the Corona print() functions are viewable via “adb logcat”, which is a great way to isolate issues on an Android device. Just do a Google search on “adb logcat”… there’s lots of information on this.

Good luck with finishing your app! [import]uid: 32256 topic_id: 19788 reply_id: 77194[/import]

I actually finished it tonight thanks to all your help! I can’t wait to release it sometime soon and show it to everyone at Ansca and the world. [import]uid: 29181 topic_id: 19788 reply_id: 77195[/import]