WebView behaviours

I’m developing an APP for display HTML pages archived on my Android device. I’m having this two problems:

  1. HTML pages are archived in the same position of main.lua file and are available when i test my app with simulator; the same pages are not available when i test the APP on my Android device. Why?
  2. When i push the “back” button on my Android device, my APP is closed. In my wishes, i want go back on the previous HTML page. Where is my mistake?
    Can you help me? Best regards.

Regarding the HTML pages, please read the Gotcha section of the system.ResourceDirectory:  https://docs.coronalabs.com/api/library/system/ResourceDirectory.html

For the back button, you will need to install a “key” event handler to catch the Android back button (https://docs.coronalabs.com/api/event/key/index.html) and call your native.newWebView()'s back method (https://docs.coronalabs.com/api/type/WebView/back.html)

Rob

Hi, reading your message, i understand it’s very complex or impossible to distribute html pages inside an APP for Android.

Is there a sample where I can study a possibile solution? If I don’t solve this problem, I must stop my work.

Best regards.

Hi, thank you, I solved my problem zipping HTML files and coping them in TemporaryDirectory.

Now, i’m working on back button. If necessary, i will ask you for a new genial idea. Best regards

I’m having some difficulties in developing of my APP.

In particular I cannot do: previous action on the web pages opened inside the browser; savage the current web pages (internal state of page), inside the browser, when change the windows (when I return on the window, the web browser restart from the initial point).

In my idea, my APP is based on two scenes; every scene contain a webpage and user can swap the windows (and the internal web page) pushing on button of a bar. Can you help me with a sample? Best regards.

I’m not sure I understand what you’re trying to do?  Can you provide some screenshots or diagrams?

Rob

ok, Rob. My application is based on: a page with a simple image an a bar menu; two other pages, identical, with a web view and the menu bar: in each page are available two different web site. The user can see the first or the second page and surf inside the two different web site. The page is selected by menu bar. In my idea the status of each web view must be indipendent and saved in every swap of pages. I used two scene and wrote my code inside the create section.

Maybe if you zip up your project and share it by Google Drive or Dropbox, the community might have a better idea of what you’re trying to accomplish.

Rob

Thank you Rob! I attached my programs hoping you can help me. Observe file view1 and view2 are identical. Best regards


– main.lua


– include Corona’s “widget” library

local widget = require “widget”

local composer = require “composer”

– show default status bar

display.setStatusBar( display.DefaultStatusBar )

display.setDefault(“background”, 1, 1, 1) 

– event listeners for tab buttons:

local function onZeroView( event )

composer.gotoScene( “view0” )

end

local function onFirstView( event )

composer.gotoScene( “view1” )

end

local function onSecondView( event )

composer.gotoScene( “view2” )

end

– create a tabBar widget with two buttons at the bottom of the screen

– table to setup buttons

local tabButtons = {

{ defaultFile=“button1.png”, overFile=“button1-down.png”, width = 32, height = 32, onPress=onZeroView },

{ defaultFile=“chisiamo.png”, overFile=“chisiamo.png”, width = 32, height = 32, onPress=onFirstView },

{ defaultFile=“galleria.png”, overFile=“galleria.png”, width = 32, height = 32, onPress=onSecondView },

}

– create the tabBar widget

local tabBar = widget.newTabBar{

top = display.contentHeight - 40, – 50 is default height for tabBar widget

buttons = tabButtons

}

– function for files copy

local function copyFile( srcName, srcPath, dstName, dstPath )

    local results = false

     – Copy the source file to the destination file

    local rFilePath = system.pathForFile( srcName, srcPath )

    local wFilePath = system.pathForFile( dstName, dstPath )

    local rfh = io.open( rFilePath, “rb” )

    local wfh, errorString = io.open( wFilePath, “wb” )

    if not ( wfh ) then

        – Error occurred; output the cause

        print( "File error: " … errorString )

        return false

    else

        – Read the file and write to the destination directory

        local data = rfh:read( “*a” )

        if not ( data ) then

            print( “Read error!” )

            return false

        else

            if not ( wfh:write( data ) ) then

                print( “Write error!” )

                return false

            end

        end

    end

    results = 2  – 2 = File copied successfully!

    – Close file handles

    rfh:close()

    wfh:close()

    return results

end

– Copy my HTML files (in zip format) from from “system.ResourceDirectory” to “system.DocumentsDirectory”

copyFile( “chisiamo.zip”, system.ResourceDirectory, “chisiamo.zip”, system.DocumentDirectory )

copyFile( “galleria.zip”, system.ResourceDirectory, “galleria.zip”, system.DocumentDirectory )

– Handle back button

function onKeyback( event )

if event.keyName == “back” then

if not ( wbeView == nil ) and webView.canGoBack then 

webView:back()

print(“go back eseguito”)

composer.removeHidden()

end

end

return true

end

– Handle onKeyBack 

Runtime:addEventListener( “key”, onKeyback )

– Open the App

onZeroView() – invoke first tab button’s onPress event manually


– view1.lua


– include Corona’s “widget” library

local composer = require( “composer” )

local scene = composer.newScene()

– set default home page on the screen

function scene:create( event )

local sceneGroup = self.view

local image = display.newImage( “postermostra.jpg” )

image.width = display.contentWidth * 0.9

image.height = ( display.contentHeight - 40) * 0.9

image.x = display.contentCenterX

image.y = (display.contentCenterY - 40)

sceneGroup:insert( image )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– composer.removeHidden()

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

if webView and webView.removeSelf then

            webView:removeSelf()

            webView = nil

        end

elseif phase == “did” then

– Called when the scene is now off screen

end

end

function scene:destroy( event )

local sceneGroup = self.view

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene


– view1.lua

– view2.lua


local composer = require( “composer” )

local scene = composer.newScene()

local webView = native.newWebView( display.contentCenterX, display.contentCenterY - 10, display.contentWidth, display.contentHeight - 90)

local widget = require( “widget” )

local bkButton = nil

– Set command line depending OS

if ( system.getInfo( “platform”) == “android” ) then

bkButton = widget.newButton(

{

width = display.contentWidth, 

height = 30,

left = 0,

top = 0,

defaultFile = “testata-android.png”,

onEvent = onbkButton,

}

)

else

bkButton = widget.newButton(

{

width = display.contentWidth, 

height = 30,

left = 0,

top = 0,

defaultFile = “testata-ios.png”,

onEvent = onbkButton,

}

)

end

– back command on webView

function onbkButton( event )

if event.phase == “began” then 

if not ( wbeView == nil ) and webView.canGoBack then 

webView:back() 

composer.removeHidden()

end

end

end

function scene:create( event )

local sceneGroup = self.view

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

– webView:request( “chisiamo/chisiamo.html”, system.DocumentDirectory )

sceneGroup:insert( webView )

sceneGroup:insert( bkButton )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

composer.removeHidden()

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

composer.removeHidden()

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

if webView and webView.removeSelf then

            webView:removeSelf()

        webView = nil

bkButton:removeSelf()

bkButton = nil

end

elseif phase == “did” then

– Called when the scene is now off screen

composer.hideOverlay()

end

end

function scene:destroy( event )

local sceneGroup = self.view

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

–scene:addEventListener( “destroy”, scene )


return scene

– view1.lua

– view2.lua

Can you zip up your project and share a dropbox link?  The code above is not useful. Dumping a lot of code, in particular, unformatted code makes it really hard to help you.  At a minimum, you should use the blue code button <> in the row with Bold/Italic etc. and paste your code into the popup window.

Ok, myAPP has been uploded on Drive: https://drive.google.com/open?id=19zxZGPbEPi2AazFAzjoPdXGw9Qua3XOt

Thank you for your cooperation.

Best regards.

It basically comes down to the fact that you’re removing your webView each time you change scenes. Then when you go back to the scene, you’re adding it again and going back to the original URL.  I suspect you’re doing this because webView’s are native objects that sit on top of Corona’s OpenGL canvas and don’t really interact with each other.  The most recent webView will be drawn on top of the previous one when switching.  I don’t know if :tofront() will work on webViews or not, but it might be worth trying. 

Anyway, you can’t remove the webView if you want to use the browser history to navigate backward.

Rob

Hi, Rob, … unfortunalely :toFront() function does not work and when I remove all “composer.removeHidden()” from my code, view1 and view2 return a white screen when I return on this views after the first round. For me it’s very strange… 

You likely will want to hide the webView and then make it visible again when you detect the scene:show() and scene:hide() events instead of removing the webView.  Try setting the .isVisible property to true and false to show and hide the webView.

Rob

Hi, Rob!

I have applied your work around but i observe this two strange effect: view1 dont refresh it’s page, then when i select this view, there is in foreground the previous; 2. the two webView view are identical. Is as if only one webView can work. It’s possibile? 

Best regards.

If you choose a different URL for each webView, they seemed to switch back and forth correctly.  If you use the same URL in both, you can’t tell which one you’re looking at.

By the way, our webView’s are not designed to be full featured. They are designed to include simple HTML content in an app that’s primarily made with Corona. I mean a small amount of content from HTML and a bulk of the content coming from the app directly.

Rob

Thx for info dude

Regarding the HTML pages, please read the Gotcha section of the system.ResourceDirectory:  https://docs.coronalabs.com/api/library/system/ResourceDirectory.html

For the back button, you will need to install a “key” event handler to catch the Android back button (https://docs.coronalabs.com/api/event/key/index.html) and call your native.newWebView()'s back method (https://docs.coronalabs.com/api/type/WebView/back.html)

Rob

Hi, reading your message, i understand it’s very complex or impossible to distribute html pages inside an APP for Android.

Is there a sample where I can study a possibile solution? If I don’t solve this problem, I must stop my work.

Best regards.

Hi, thank you, I solved my problem zipping HTML files and coping them in TemporaryDirectory.

Now, i’m working on back button. If necessary, i will ask you for a new genial idea. Best regards