Android Question Display message in OverrideUrl

cdsincfl

Member
Licensed User
Longtime User
Using B4A 10.5
I am attempting to show a message in a webview overrideurl sub as in this code:

B4X:
Dim result As Int
    result = Msgbox2("Edit Selected Item?", "", "Yes", "", "No", Null)
    If result = DialogResponse.POSITIVE Then
        ' do something
        Return True
    Else
        ' do something
        Return True   
    End If

This code lock my app and then the app shuts down.
I tried using Msgbox2Async but that causes the sub to be resumable so that didn't work.

How can I show a confirmation message in the WebView_OverrideURL?
 

drgottjr

Expert
Licensed User
Longtime User
i don't think you want to get into an interactive
dialog in that sub.

since you always return true, there is no need to
hang around in the sub (especially since webview
is already waiting for you to finish hanging around).
trigger another sub (callsubdelayed) with the non-modal
dialog in it and return true from the event sub. the user's
decision is then made elsewhere, away from the event sub.

in looking at the tiny snippet you posted, not only
were you waiting for the modal dialog, but the
"do something" means that the sub could
be active even longer before returning true.
i think that's a bad move.

it looks to me like you're using the overrideurl event
for something it was not designed to handle.
šŸ‘for the clever hack, but you lose points by not
exiting the sub as quickly as possible.

i'm going to try to cobble something together out of
curiosity, but you might want to think about what i'm
suggesting.

there are other ways to get the kind of communication
you're looking for between webview and app. you picked
a tricky one, in my opinion, but i think it should work.

---------------------------------------------------------------
UPDATE: yeah, it works as i thought. so, first, there could
be errors elsewhere in your code; you only showed a very
small part.

anyway, here's what i did:
i created a sub with a msgbox2 dialog in it (use async if you
want). in overrideurl i put a log message indicating that i
was in overrideurl. i added a callsubdelayed() to the sub with
the dialog next, and then immediately followed with return true
(actually, i put return false because i wanted to see if the link
was followed before or after the dialog).

i had a webpage in a webview click on a link which, naturally,
triggers an overrideurl event in b4a. the link was followed
(meaning the sub had returned false), and then the dialog
appeared. no crash.

so pseudo-code:
B4X:
sub overrideurl
   log("in overrideurl")
   callsubdelayed(Me, "dialogsub")           ' i hovered in the dialog.  no effect on the overrideurl sub
   return true    ' or false     this will not be delayed
end sub

sub dialogsub
   dim ans as int = msgbox2( .......)        ' or msgbox2async() and wait for
   if ans = dialogresponse.positive then
       ....
   else
       ....
   end if
end sub

to repeat myself, there could be some other issue in your code which causes your crash. try the above.
 
Last edited:
Upvote 0

cdsincfl

Member
Licensed User
Longtime User
Thanks drgottjr.
I will try the code you suggested.
The original code never went anywhere, it just did a Toast message to indicate the code flow.

I figured it could be done using JavaScript but thought there had to be a simpler solution.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
don't use toast; use the log. yeah, injecting javascript is another approach. in a way, it's very similar to what you did, and - mainly - you would avoid having to return a value back to webview. having dialogs in a sub which webview is waiting to hear back from may be problematic. i wouldn't want to take the time to prove or disprove it. i just wouldn't do it.
 
Upvote 0

cdsincfl

Member
Licensed User
Longtime User
Thanks for the help.
I used your suggestion and it works great.
Here is the code I used:

B4X:
Sub wbvWebView_OverrideUrl (Url As String) As Boolean
    ' Set some values
    Dim values() As String
    values = Regex.Split("[.]", Url.SubString(7))
    Dim row As Int
    row = values(1)
    CurrentINVIndex = row
    CallSubDelayed(Me, "MySub")
    Return True
End Sub

Sub MySub   
    Msgbox2Async("Select Action: ", "Confirm", "Edit", "Cancel", "Delete", Null, False)
    Wait For Msgbox_Result (result As Int)
    If result = DialogResponse.POSITIVE Then
        ' Edit
        Log("Action Selected: Edit")
        Return
    Else If result = DialogResponse.NEGATIVE Then
        ' Delete
        Log("Action Selected: Delete")
        Return   
    Else
        ' Cancelled
        Log("Action Selected: Cancelled")
        Return
    End If
    
End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i would be happier if you just passed url to your new sub. since you always return true, there is no decision making required in the overrideurl sub.
make your delayed call (passing url) and get out. i'm not even sure you have to use the delayed version. you could try with just a standard callsub with 1 argument. the delayed version just sounds a little safer to me, but if you're going to use this kind of thing again, you might want to know where you stand. webview is a tricky fellow:


B4X:
Sub wbvWebView_OverrideUrl (Url As String) As Boolean

    CallSubDelayed(Me, "MySub", Url)
    Return True
End Sub

Sub MySub(String Url)
    ' Set some values
    Dim values() As String
    values = Regex.Split("[.]", Url.SubString(7))
    Dim row As Int
    row = values(1)
    CurrentINVIndex = row

    Msgbox2Async("Select Action: ", "Confirm", "Edit", "Cancel", "Delete", Null, False)
    Wait For Msgbox_Result (result As Int)
    If result = DialogResponse.POSITIVE Then
        ' Edit
        Log("Action Selected: Edit")
        Return
    Else If result = DialogResponse.NEGATIVE Then
        ' Delete
        Log("Action Selected: Delete")
        Return  
    Else
        ' Cancelled
        Log("Action Selected: Cancelled")
        Return
    End If
   
End Sub

let me just add, i think your goal should be to not let anything unexpected occur in the overrideurl sub. even a simple string parse could cause an exception and leave webview hanging. you have no control over webview. sometimes it operates on the main thread, sometimes not. it's in its own universe. it is very big. be afraid.
 
Last edited:
Upvote 0
Top