Back to the start-layout

epistula

Member
Licensed User
Longtime User
Hi all,

I use a small program with buttons. Each button opens a different webview. In the webviews i go back with the back button with this:

B4X:
Sub activity_KeyPress (KeyCode As Int) As Boolean
  Select keycode
     Case KeyCodes.KEYCODE_BACK: 
      page_back               
      Return True
  End Select
End Sub

Sub page_back
webview1.Back
End Sub

My problem is now that i want to go back to the opening screen with the back button. Is there a way to go back from the webviews to the start screen with the back button?

Thanks!
 
Last edited:

adamioan

Member
Licensed User
Longtime User
Why don't you check the URL when the back is pressed? If its the initial URL then close the web view else call the webview1.back
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
This should work for you, if you have a Button1, WebView1
on a layout named: "i"


B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

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 WebView1 As WebView
   Dim Button1 As Button
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("i")
   WebView1.LoadHtml("<html><body>Hello world!</body></html>")
   WebView1.Visible = False

button1.text="Google"


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub WebView1_PageFinished (Url As String)
   
End Sub
Sub WebView1_OverrideUrl (Url As String) As Boolean
   
End Sub
Sub WebView1_UserAndPasswordRequired (Host As String, Realm As String) As String()
   
End Sub
Sub Button1_down


If WebView1.Url = "http://www.google.com/" Then
   WebView1.Visible = False
   Button1.text = "Google"
   WebView1.LoadUrl("about:page")
   Else
   WebView1.Visible = True
   Button1.text = "Back"
   WebView1.LoadUrl("http://www.google.com")
   End If
   
   
   
   
   
End Sub
Sub Button1_Click
   
End Sub
 
Upvote 0

epistula

Member
Licensed User
Longtime User
Thank's, but it's not working correct here. I want a solution with the android back button, not with an own back button.
 
Upvote 0

epistula

Member
Licensed User
Longtime User
The check url idea was the solution. now it works nice:

B4X:
Sub page_back
If webview1.Url = ("URL1") OR webview1.Url = ("URL2") Then
webview1.RemoveView
Else
webview1.Back
End If
End Sub

Thank's to all!
 
Upvote 0

adamioan

Member
Licensed User
Longtime User
Here is a simple code
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

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 wvWeb1_URL, wvWeb2_URL As String 
   Dim btnButton1, btnButton2 As Button 
   Dim wvWeb1, wvWeb2 As WebView 
End Sub

Sub Activity_Create(FirstTime As Boolean)

   btnButton1.Initialize("btnButton1")
   Activity.AddView (btnButton1, 0,0, 45%x, 50)
   btnButton1.Text="WebView1"
   
   btnButton2.Initialize("btnButton2")
   Activity.AddView (btnButton2, 55%x,0, 45%x, 50)
   btnButton2.Text="WebView2"
   
   wvWeb1.Initialize("wvWeb1")
   Activity.AddView(wvWeb1, 0, 55, 100%x, 90%y - 55)
   wvWeb1.Visible=False
   
   wvWeb2.Initialize("wvWeb2")
   Activity.AddView(wvWeb2, 0, 55, 100%x, 90%y - 55)
   wvWeb2.Visible=False
   
   wvWeb1_URL="http://www.b4x.com"
   wvWeb2_URL="http://www.greekfunzone.com"
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub activity_KeyPress (KeyCode As Int) As Boolean
  If KeyCode = KeyCodes.KEYCODE_BACK Then
'      Msgbox("wv1_url=" & wvWeb1_URL & CRLF & "url=" & wvWeb1.Url , "wv2_url=" & wvWeb2_URL & " - " & "url=" & wvWeb2.Url )
      If wvWeb1.Visible =True Then
         If wvWeb1.Url=wvWeb1_URL OR wvWeb1.Url=wvWeb1_URL & "/" Then 
            wvWeb1.Visible=False
         Else
            wvWeb1.Back 
         End If
         Return True
      Else
         If wvWeb2.Visible =True Then
            If wvWeb2.Url=wvWeb2_URL OR wvWeb2.Url=wvWeb2_URL & "/" Then 
               wvWeb2.Visible=False
            Else
               wvWeb2.Back 
            End If
            Return True
         End If
      End If
   End If
End Sub

Sub btnButton1_Click
   wvWeb2.Visible=False
   wvWeb1.LoadUrl(wvWeb1_URL)
   wvWeb1.Visible=True
End Sub

Sub btnButton2_Click
   wvWeb1.Visible=False
   wvWeb2.LoadUrl(wvWeb2_URL)
   wvWeb2.Visible=True
End Sub
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
My problem Is now that i want To go back To the opening screen with the back Button. Is there a way To go back from the webviews To the start screen with the back Button?

Thanks!

Just a thought, but couldn't you code it so if the back button is pressed, it goes backwards in the webview, but if the back button is pressed twice in a certain interval, say within one second of the original press, it exits to the app? So basically if you single click back it does webview if you double click it exits.

I have used this in a few apps and it seems to work fine
Something like this:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim TimePushed As Long
   Dim BackPushedAlready As Boolean
End Sub
Then the code for the keypress itself would be:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If keycode = KeyCodes.KEYCODE_BACK Then
      If BackPushedAlready = True Then
         If DateTime.Now - TimePushed <= 1000 Then 
            Return False
         Else
            BackPushedAlready = False
            Page_Back
            Return True
         End If
      End If
      If BackPushedAlready = False Then
         BackPushedAlready = True
         TimePushed = DateTime.Now
         Page_Back
         Return True
      End If
   End If 
   
   
   
End Sub
Then your Page_back sub
B4X:
Sub Page_Back
webview1.Back
End Sub

Then if you single click the back button, it will go back one page in the webview, continuing one page every time the back button is pushed, all the way back to the beginning. Then to exit just double click the back button or hold it for a second.

This way you have the option to either go back one webview page or exit the app without having to go all the way back to the beginning, because if you have a lot of webviews then its gonna take a little bit to get back to the beginning.

Thatis just my thoughts on it.

Good luck
George
 
Last edited:
Upvote 0

cammel8

Member
Licensed User
Longtime User
Just realised you might be talking exiting to the main page of the app and not exiting app. Even still, the same code would work but instead of putting exit code to exit the app, put code directing the app to where you want to go in its place.
 
Upvote 0

epistula

Member
Licensed User
Longtime User
@cammel8
Great code, thank's! I changed TimePushed to 600, because i'm a fast one clicker .

@all
I realised the whole program now with different activities, every webview runs in an own activity. So i can use my page_back sub with webview1.Back or Activity.Finish and in the main-activity i can use the android back-button to close the program.

But i have only one more problem. When i open an webview and it needs a longer load-time and i press then the android-back-button i get a java.lang.NullPointerException.

Any ideas how to change my page_back (attached) sub for this case?

B4X:
Sub page_back

If webview1.Url = ("URL1") OR _
webview1.Url = ("URL2") Then
   Activity.Finish
Else
   webview1.Back
End If

End Sub


Thanks for the great support here!
 
Upvote 0

epistula

Member
Licensed User
Longtime User
The PageFinished event sounds good and i tried it here, but i have no success, only errors. It can be that i don't understand how this PageFinished event works. Where can i find a litte example to understand how this event works?
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Ok I had been having problems with nullpointerexceptions on a app I wrote. Here is what I found out:

Basically when you declare a reference variable (an object), you are really only creating a pointer. Its not like a primitive type variable.

Here is what I mean.

If you were to do this line of code
B4X:
dim mynum as int
mynum = 2

Technically mynum was set to 0, then set to 10 in the second line. 10 is then put into the memory area designated by mynum

Now lets say, instead of setting it to 10, you set mynum to pull from an array that has yet to be created. Instead of setting it to 0 it sets it to nothing (IE:null) then points it to the array to get its insantiation.

The null pointer exception is thrown when you declare a variable but don't create the object for the variable. The app is expecting something where there is nothing.

Now by what you were saying it seems that you are forcing the webview to quit before instantiating the variable and hence the NullPointerException.

Couldn't you just write a catch that ,if the null is thrown, it ignores it.

Now this is just a guess, Im not sure if it would work but it might. But maybe something like this coded into your back button:
B4X:
if mynum = null then 
mynum=0 'or muynum = (whatever it is supposed to hold) so it isnt empty
'CODE FOR BACK BUTTON COPIED HERE 
else 
'CODE FOR BACK BUTTON COPIED HERE 
end if
The idea being if mynum is null, instantiate it with some kind of data like it would have received had you not stopped the process. And if it is istantiated then just continue with the back button code.

That or if the code that mynum is supposed to hold does not matter after the back button is pushed, just write a line of code at the beginning of the back button sub that states:
B4X:
Mynum=0
this way as soon as you push the back button it fills the variable and moves on.

Like I said I have no idea if it will work, but I would think that if you caught the empty variable before you threw the exception you would be able to stop it from throwing it.

That is about the only thing i can think of.

Thanx
George
 
Upvote 0

epistula

Member
Licensed User
Longtime User
@cammel8
Thanks for your answer, but this is not working fore for me (or i don't unterstand it ).

Back to my question. Can sombody give me an little example how the PageFinished event works?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…