Android Question Persist Webview through Orientation Change

DawningTruth

Active Member
Licensed User
This is with reference to: https://www.b4x.com/android/forum/t...hrough-orientation-change.104100/#post-652854

How do I use StateManager to preserve a WebView through orientation change. In particular I need to preserve the webview backforward history. I have attached a sample project which has both WebView and StateManager attached.

Please advise how to modify it so that it can also preserve the WebView state.
 

Attachments

  • WebviewOrientation.zip
    11.1 KB · Views: 127

Erel

Administrator
Staff member
Licensed User
StateManager doesn't do anything with WebView.

Example of saving WebView state when the orientation changes:
B4X:
Sub Process_Globals
   Private Bundle As JavaObject
End Sub

Sub Globals
   Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   
   If Bundle.IsInitialized Then
       Dim jo As JavaObject = WebView1
       jo.RunMethod("restoreState", Array(Bundle))
   Else
       WebView1.LoadUrl("https://www.b4x.com")
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Bundle.InitializeNewInstance("android.os.Bundle", Null)
   Dim jo As JavaObject = WebView1
   jo.RunMethod("saveState", Array(Bundle))
End Sub


Sub btnBack_Click
   WebView1.Back
End Sub

Sub btnForward_Click
   WebView1.Forward
End Sub
 
Upvote 0

DawningTruth

Active Member
Licensed User
StateManager doesn't do anything with WebView.

Example of saving WebView state when the orientation changes:
B4X:
Sub Process_Globals
   Private Bundle As JavaObject
End Sub

Sub Globals
   Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
  
   If Bundle.IsInitialized Then
       Dim jo As JavaObject = WebView1
       jo.RunMethod("restoreState", Array(Bundle))
   Else
       WebView1.LoadUrl("https://www.b4x.com")
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Bundle.InitializeNewInstance("android.os.Bundle", Null)
   Dim jo As JavaObject = WebView1
   jo.RunMethod("saveState", Array(Bundle))
End Sub


Sub btnBack_Click
   WebView1.Back
End Sub

Sub btnForward_Click
   WebView1.Forward
End Sub

Trying to adapt this to my code but stumped.

How would you do this if you had 2 x webviews in the activity?

B4X:
'Pseudocode

Private WebView1 As WebView
Private WebView2 As WebView

WebView1.LoadURL("http://www.website1.com")
WebView2.LoadURL("http://www.website2.com")
 
Upvote 0

DawningTruth

Active Member
Licensed User
You can use two bundles or save the two WebViews in the same bundle (I haven't tested it).
Ok, tested it and it works with 2 different bundles.

Now I'm getting a weird error. Let me explain:

I load the Views - works
Rotate the phone - works
Rotate back - crashes

What is weird is the error. It is like it has lost the bundle or something.

Here are the logs:

5fP6No.jpg

Here is the code for lines 69 and 70.

B4X:
Dim jo As JavaObject = webview1
jo.RunMethod("saveState", Array(Bundle1))
 
Upvote 0

DawningTruth

Active Member
Licensed User
Ok, tested it and it works with 2 different bundles.

Now I'm getting a weird error. Let me explain:

I load the Views - works
Rotate the phone - works
Rotate back - crashes

What is weird is the error. It is like it has lost the bundle or something.

Here are the logs:

5fP6No.jpg

Here is the code for lines 69 and 70.

B4X:
Dim jo As JavaObject = webview1
jo.RunMethod("saveState", Array(Bundle1))

Ok, got it to work. Was a bug specific to my code. I was not carrying the control variables across on the reload.
 
Last edited:
Upvote 0
Top