native.newWebView bug with hardware back key!

Hi,

I am creating a native.newWebview and each time I click on the Android hardware “back” key, the app quit gracefully!.

I try overiding key event with Runtime:addEventListener( “key”, onKeyEvent ), but no luck. The callback method is not even call, as the app quit before that.

Any clue of how I can prevent the app to quit when a native.newWebview is shown?

Thanks,

It’s not a bug.  When displaying a native WebView, you have to handle WebView navigation with the"back" key yourself.  This is especially true for apps that display multiple WebViews, because Corona would have no idea which WebView should accept the “back” key.

Have a look at the sample code via the link below on how to receive and override the “back” key event.

   http://docs.coronalabs.com/daily/api/event/key/keyName.html

Once you receive the “back” key (and you should only handle the “down” phase), then you should then call the WebView’s back() function… or destroy the WebView if it has no more history by calling its canGoBack() function.  It’s up to you to decide on that behavior.

   http://docs.coronalabs.com/daily/api/type/WebView/back.html

Alternatively, you can use our old native.showPopup() API instead.  That does automatically handle the back key because only one web popup is allowed onscreen at a time.  Pressing the back key will cause it to go back in its history, and if it has no more history, then it will be closed.

   http://docs.coronalabs.com/daily/api/library/native/showPopup.html

   http://docs.coronalabs.com/daily/api/library/native/cancelWebPopup.html

@joshua. I know that I need to overide the “Runtime:addEventListener( “key”, onKeyEvent );” key myself.

The problem is when the newWebView is created, we do not receive any key even on the callback. That’s the problem.

The key events are working for me.  Perhaps your callback has a Lua runtime error in it that’s preventing it from working.  Try enabling our “showRuntimeErrors” option in your “config.lua” to see if this is the case.

   http://docs.coronalabs.com/guide/basics/configSettings/index.html#runtime-errors

Also, try running sample app “Hardware/KeyEvents” that is included with the Corona Simulator on your Android device.  It will display all key presses onscreen.

There is no runtime error. We also did a small app directly in the main.lua (2013.1148).

With this code, The onKeyEvent is not called on any of our android hardware we have. (if you comment the last 3 lines, the onKeyEvent callback will be called!)

local function onKeyEvent( event )

    print(“onKeyEvent”, event.keyName, event.phase )

    if event.keyName == “back” then

    – do not quit!

        return true

    end       

   return false

end

Runtime:addEventListener( “key”, onKeyEvent )

local function webListener( event )

    print(“webListener”)

end

local webView = native.newWebView( 0,0,200,300)

webView :addEventListener( “urlRequest”, webListener )

webView :request( “http://www.google.com” )

It’s not a bug.  When displaying a native WebView, you have to handle WebView navigation with the"back" key yourself.  This is especially true for apps that display multiple WebViews, because Corona would have no idea which WebView should accept the “back” key.

Have a look at the sample code via the link below on how to receive and override the “back” key event.

   http://docs.coronalabs.com/daily/api/event/key/keyName.html

Once you receive the “back” key (and you should only handle the “down” phase), then you should then call the WebView’s back() function… or destroy the WebView if it has no more history by calling its canGoBack() function.  It’s up to you to decide on that behavior.

   http://docs.coronalabs.com/daily/api/type/WebView/back.html

Alternatively, you can use our old native.showPopup() API instead.  That does automatically handle the back key because only one web popup is allowed onscreen at a time.  Pressing the back key will cause it to go back in its history, and if it has no more history, then it will be closed.

   http://docs.coronalabs.com/daily/api/library/native/showPopup.html

   http://docs.coronalabs.com/daily/api/library/native/cancelWebPopup.html

@joshua. I know that I need to overide the “Runtime:addEventListener( “key”, onKeyEvent );” key myself.

The problem is when the newWebView is created, we do not receive any key even on the callback. That’s the problem.

The key events are working for me.  Perhaps your callback has a Lua runtime error in it that’s preventing it from working.  Try enabling our “showRuntimeErrors” option in your “config.lua” to see if this is the case.

   http://docs.coronalabs.com/guide/basics/configSettings/index.html#runtime-errors

Also, try running sample app “Hardware/KeyEvents” that is included with the Corona Simulator on your Android device.  It will display all key presses onscreen.

There is no runtime error. We also did a small app directly in the main.lua (2013.1148).

With this code, The onKeyEvent is not called on any of our android hardware we have. (if you comment the last 3 lines, the onKeyEvent callback will be called!)

local function onKeyEvent( event )

    print(“onKeyEvent”, event.keyName, event.phase )

    if event.keyName == “back” then

    – do not quit!

        return true

    end       

   return false

end

Runtime:addEventListener( “key”, onKeyEvent )

local function webListener( event )

    print(“webListener”)

end

local webView = native.newWebView( 0,0,200,300)

webView :addEventListener( “urlRequest”, webListener )

webView :request( “http://www.google.com” )