Let me update this thread with my (lack of) progress!
I've attached a sample project to this post, here's the code:
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ScrollX, ScrollY As Float
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Reflector1 As Reflector
Dim WebView1 As WebView
Dim WebViewXtender1 As WebViewXtender
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ScrollX=0
ScrollY=0
End If
WebView1.Initialize("WebView1")
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
WebView1.LoadUrl("http://www.b4x.com/forum/basic4android-updates-questions/14185-how-save-current-url-page-position.html")
Reflector1.Target=WebView1
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Dim ContentHeight As Int
Dim Scale As Float
Log("Activity_Pause")
If UserClosed=False Then
ContentHeight=Reflector1.RunMethod("getContentHeight")
If ContentHeight>0 Then
Scale=WebViewXtender1.getScale(WebView1)
Log("Activity_Pause Scale: "&Scale)
ScrollX=WebViewXtender1.getScrollX(WebView1)
ScrollY=WebViewXtender1.getScrollY(WebView1)
Log("Activity_Pause Absolute (pixel) position: "&ScrollX&", "&ScrollY)
ScrollX=ScrollX/Scale
ScrollY=ScrollY/Scale
Log("Activity_Pause Scaled saved position: "&ScrollX&", "&ScrollY)
Else
Log("Activity_Pause ContentHeight is ZERO, saved position NOT updated")
End If
End If
End Sub
Sub WebView1_PageFinished (Url As String)
Dim ContentHeight As Int
Dim Scale As Float
Log("WebView1_PageFinished")
ContentHeight=Reflector1.RunMethod("getContentHeight")
If ContentHeight=0 Then
Log("WebView1_PageFinished ContentHeight is ZERO do not try to restore scroll position")
End If
If ContentHeight>0 AND (ScrollX<>0 OR ScrollY<>0) Then
Scale=WebViewXtender1.getScale(WebView1)
Log("WebView1_PageFinished Scale: "&Scale)
ScrollX=ScrollX*Scale
ScrollY=ScrollY*Scale
WebViewXtender1.scrollTo(WebView1, ScrollX, ScrollY)
Log("WebView1_PageFinished Restored scaled position: "&ScrollX&", "&ScrollY)
ScrollX=0
ScrollY=0
End If
End Sub
I'm using Roeschti's
WebViewXtended library to get the WebView's ScrollX, ScrollY and Scale properties.
And i'm using the Reflection library to get the WebView's ContentHeight.
The whole process of saving and restoring the WebView's scroll position is hurdle after hurdle.
Getting and setting the WebView's scroll position is no real technical problem BUT there are practical problems...
PROBLEM: With a local HTML webpage in android assets the Sub WebView1_PageFinished is
generally(!) executed before the webpage has been rendered and the WebView has a ContentHeight of zero at that point.
A webpage loaded over the internet takes longer to load and render so by the time the Sub WebView1_PageFinished is executed the WebView does have a ContentHeight of more than zero pixels.
While the WebView's ContentHeight is zero pixels there is no point restoring the last scroll position.
If the ContentHeight is zero in Sub WebView1_PageFinished the code now needs to wait until the ContentHeight is more than zero (the webpage has now been rendered) and then restore the scroll position.
PROBLEM: The default behaviour of a WebView is to load and render a webpage at full zoom (not in "overview mode").
See
Targeting Screens from Web Apps | Android Developers.
Full zoom seems to vary with portrait and landscape aspect ratios so the saved scroll position of a WebView from one orientation will not exactly match the desired scroll position to restore in the other orientaion.
If you have control over the webpages that you are loading then you can set the viewport meta tag in those pages so as to better target the mobile browser, here's the attributes available with the Android WebView:
<meta name="viewport"
content="
height = [pixel_value | device-height] ,
width = [pixel_value | device-width ] ,
initial-scale = float_value ,
minimum-scale = float_value ,
maximum-scale = float_value ,
user-scalable = [yes | no] ,
target-densitydpi = [dpi_value | device-dpi |
high-dpi | medium-dpi | low-dpi]
" />
I experimented with the viewport meta tag on a local webpage in android assets but as the Sub WebView1_PageFinished was tending to execute before the webpage had been rendered had no success at getting the webpage to render consistently in both portrait and landscape orientation.
The attached project does not work perfectly - it restores the scroll position approximately.
I did previously have the code so that it was very nearly perfect but made some changes and couldn't revert back to the nearly working version after i'd made those changes! :signOops:
I'll experiment more later and keep this thread updated.
Martin.