Spanish [SOLUCIONADO] como evito que mi WebView se reinicie al rotar la pantalla

cpedgarbm

Member
Licensed User
Amigos les suplico su ayuda en esto: Tengo corriendo un webview con contenido html, al rotar la pantalla se reinicia mi aplicación, la duda es como podría conservar mi webview. es decir que al rotar la pantalla solo se redimencione el webview sin que afecte su contenido.
 

bgsoft

Well-Known Member
Licensed User
Longtime User
Hola

Guardate la dirección actual en una variable global, cuando entres en Create, comprueba que estuviera activo el WebView, y si lo está lo vuelves a cargar con esa dirección.

Lo mejor para evitar estas cosas es crear una carga inicial desde el Create que cargue todo lo que se necesita, asi cuando gira se pondrá todo bien.

Saludos
 

rscheel

Well-Known Member
Licensed User
Longtime User
Estimado, el statemanger no sirve para la rotación del webview yo algún tiempo atrás me enfrente al mismo problema le dejo un código de ejemplo para que lo use en su proyecto.

En el manifest editor agregar la siguiente linea

B4X:
SetActivityAttribute(Main, android:configChanges, "orientation|keyboardHidden|screenSize")

El código para la aplicación es el siguiente.

B4X:
#Region  Project Attributes
    #ApplicationLabel: Ejemplo WebView
    #VersionCode: 1
    #VersionName: 1.0.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Obj1 As Reflector
End Sub

Sub Globals
    Private WView As WebView
    Dim WebViewExt As WebViewExtras
    Private ActivityParent As JavaObject
    Private ime1 As IME
    'Dim imei As PhoneId
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("webview")
    Activity.Title = "Ejemplo WebView"
    WebViewExt.Initialize(WView)
    WebViewExt.GetSettings.SetDisplayZoomControls(False)
    WebViewExt.ZoomEnabled = False
    WebViewExt.ClearCache(True)

    'If FirstTime Then
      If isConnected Then
          WView.LoadUrl("La direccion web de lo que va a cargar en el webview")
  
        Obj1.Target = WebViewExt
      Else
            Msgbox("Para usar la aplicación necesita conexión a internet.", "Conexión Internet")
            ExitApplication
      End If
    'End If

    ime1.Initialize("ime1")
    ime1.AddHeightChangedEvent
    Dim jo As JavaObject = Activity
    jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
    Array As Object(0x20))
    ActivityParent = jo.RunMethodJO("getParent", Null)
End Sub

Sub IME1_HeightChanged (NewHeight As Int, OldHeight As Int)
       CallSubDelayed(Me, "AfterChange")
End Sub

Sub AfterChange
       Dim ajo As Panel = Activity
       Dim width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
       Dim height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
       If width = 0 Or height = 0 Then Return
       ajo.Width = width 'update the "activity" width and height
       ajo.Height = height
       WView.Width = width
       WView.Height = height
End Sub

Sub isConnected() As Boolean
    Dim sSocket As ServerSocket
If  sSocket.IsInitialized = False Then
    sSocket.Initialize(8080, "sSocket")
    sSocket.Close
End If
    Dim res As String
    res = sSocket.GetMyIP
    Log(res)
If sSocket.GetMyIP = "127.0.0.1" Or res.StartsWith("fe80::") Then 'fe80::94c2:c6ff:fe42:b881%dummy0
    Return False
Else
       Return True
End If
End Sub

Sub Activity_Resume
    AfterChange
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
    End If
    Return True
End Sub

Las librerías que tiene que usar son las siguientes, AppCompat y phone no es necesaria.

Captura.PNG

Espero te sirva saludos.
 

cpedgarbm

Member
Licensed User
En el ejemplo que me compartes rscheel no me funciona, pues el contenido (URL) se vuelve a actualizar, perdiendo algún formulario abierto etc. o estoy haciendo algo mal:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Ejemplo WebView
    #VersionCode: 1
    #VersionName: 1.0.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Obj1 As Reflector
End Sub

Sub Globals
    Private WView As WebView
    Dim WebViewExt As WebViewExtras
    Private ActivityParent As JavaObject
    Private ime1 As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Activity.LoadLayout("webview")
    Activity.Title = "Ejemplo WebView"
    WView.Initialize("WView")
    WebViewExt.Initialize(WView)
    WebViewExt.GetSettings.SetDisplayZoomControls(False)
    WebViewExt.ZoomEnabled = False
    WebViewExt.ClearCache(True)
    Activity.AddView(WView,0,0,100%x,100%y)
    'If FirstTime Then
    Log("estaconectado: " & isConnected)
    If isConnected Then
          WView.LoadUrl("http://www.google.com.mx")
        Obj1.Target = WebViewExt
    Else
        Msgbox("Para usar la aplicación necesita conexión a internet.", "Conexión Internet")
        ExitApplication
    End If
    'End If

    ime1.Initialize("ime1")
    ime1.AddHeightChangedEvent
    Dim jo As JavaObject = Activity
    jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
    Array As Object(0x20))
    ActivityParent = jo.RunMethodJO("getParent", Null)
End Sub

Sub IME1_HeightChanged (NewHeight As Int, OldHeight As Int)
       CallSubDelayed(Me, "AfterChange")
End Sub

Sub AfterChange
       Dim ajo As Panel = Activity
       Dim Width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
       Dim Height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
       If Width = 0 Or Height = 0 Then Return
       ajo.Width = Width 'update the "activity" width and height
       ajo.Height = Height
       WView.Width = Width
       WView.Height =Height
End Sub

Sub isConnected() As Boolean
    Dim sSocket As ServerSocket
    If  sSocket.IsInitialized = False Then
        sSocket.Initialize(8080, "sSocket")
        sSocket.Close
    End If
    Dim res As String
    res = sSocket.GetMyIP
    Log(res)
    If sSocket.GetMyIP = "127.0.0.1" Or res.StartsWith("fe80::") Then 'fe80::94c2:c6ff:fe42:b881%dummy0
        Return False
    Else
        Return True
    End If
End Sub

Sub Activity_Resume
    AfterChange
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
    End If
    Return True
End Sub
 

rscheel

Well-Known Member
Licensed User
Longtime User
En el ejemplo que me compartes rscheel no me funciona, pues el contenido (URL) se vuelve a actualizar, perdiendo algún formulario abierto etc. o estoy haciendo algo mal:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Ejemplo WebView
    #VersionCode: 1
    #VersionName: 1.0.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Obj1 As Reflector
End Sub

Sub Globals
    Private WView As WebView
    Dim WebViewExt As WebViewExtras
    Private ActivityParent As JavaObject
    Private ime1 As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Activity.LoadLayout("webview")
    Activity.Title = "Ejemplo WebView"
    WView.Initialize("WView")
    WebViewExt.Initialize(WView)
    WebViewExt.GetSettings.SetDisplayZoomControls(False)
    WebViewExt.ZoomEnabled = False
    WebViewExt.ClearCache(True)
    Activity.AddView(WView,0,0,100%x,100%y)
    'If FirstTime Then
    Log("estaconectado: " & isConnected)
    If isConnected Then
          WView.LoadUrl("http://www.google.com.mx")
        Obj1.Target = WebViewExt
    Else
        Msgbox("Para usar la aplicación necesita conexión a internet.", "Conexión Internet")
        ExitApplication
    End If
    'End If

    ime1.Initialize("ime1")
    ime1.AddHeightChangedEvent
    Dim jo As JavaObject = Activity
    jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
    Array As Object(0x20))
    ActivityParent = jo.RunMethodJO("getParent", Null)
End Sub

Sub IME1_HeightChanged (NewHeight As Int, OldHeight As Int)
       CallSubDelayed(Me, "AfterChange")
End Sub

Sub AfterChange
       Dim ajo As Panel = Activity
       Dim Width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
       Dim Height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
       If Width = 0 Or Height = 0 Then Return
       ajo.Width = Width 'update the "activity" width and height
       ajo.Height = Height
       WView.Width = Width
       WView.Height =Height
End Sub

Sub isConnected() As Boolean
    Dim sSocket As ServerSocket
    If  sSocket.IsInitialized = False Then
        sSocket.Initialize(8080, "sSocket")
        sSocket.Close
    End If
    Dim res As String
    res = sSocket.GetMyIP
    Log(res)
    If sSocket.GetMyIP = "127.0.0.1" Or res.StartsWith("fe80::") Then 'fe80::94c2:c6ff:fe42:b881%dummy0
        Return False
    Else
        Return True
    End If
End Sub

Sub Activity_Resume
    AfterChange
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
    End If
    Return True
End Sub


Agregaste esta linea en el Manifest editor?

B4X:
SetActivityAttribute(Main, android:configChanges, "orientation|keyboardHidden|screenSize")
 

cpedgarbm

Member
Licensed User
Rscheel eres un genio amigo, solucioné mi problema, gracias mil por tu apoyo, efectivamente lo había agregado al manifest pero por alguna razón no se había guardado. funciona al 100%
 
Top