Android Question Back Button with WebView

Alansari

Member
Licensed User
Longtime User
Hi all

I have 4 html files:
index.html
1.html
2.html
3.html

I want when I in 1,2,3 pages the back button is trun back to index.html
if i in index.html i want the button to exit the app

how to do that ???

I try this code but only turn back to index page
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
   
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        WebView1.Back
        Return True
    End If
   
    Return False

End Sub
 

ronell

Well-Known Member
Licensed User
Longtime User
if i in index.html i want the button to exit the app
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
            Dim result As Int
            result = Msgbox2("Exit App?", "", "yes", "", "no", Null)
  
            If result = DialogResponse.POSITIVE Then
                            Activity.Finish
              
  
            Else If result = DialogResponse.NEGATIVE Then
  
              
            End If                      
        Return True                                           
    Else
        Return False
    End If
End Sub
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Adjust the URLs to your needs.


B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
  
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If webview1.Url = "index.html" Then
            Activity.Finish
        Else
            webview1.LoadUrl("index.html")
            'or
            webview1.Back
        End If

        Return True
    End If
  
    Return False

End Sub
 
Upvote 0

Alansari

Member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
            Dim result As Int
            result = Msgbox2("Exit App?", "", "yes", "", "no", Null)
 
            If result = DialogResponse.POSITIVE Then
                            Activity.Finish
             
 
            Else If result = DialogResponse.NEGATIVE Then
 
             
            End If                     
        Return True                                          
    Else
        Return False
    End If
End Sub

This code exit the app from any page

Only from index page I want to exit app
But from other pages I want to back to index page
 
Upvote 0

Alansari

Member
Licensed User
Longtime User
Adjust the URLs to your needs.


B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
 
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If webview1.Url = "index.html" Then
            Activity.Finish
        Else
            webview1.LoadUrl("index.html")
            'or
            webview1.Back
        End If

        Return True
    End If
 
    Return False

End Sub

no close the activity but return to index page
 
Upvote 0
Top