Android Question WebView Instance

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I'm facing a problem here with webviews: when running a mediaelement inside a WebView, to finish the activity isn't finishing the streaming. And when starting again the activity I'm getting two instances of streaming, many times blocking each other and generating strange behaviors.
How can I finish the webview media process running inside a webview?
I tested to load a blank page before finishing or pausing the activity but this generates a limitation to a very interesting use that is the capacity of a webview to run in background (at least for some time), maintaining for example a music when the user checks other apps or screens.
So, I must to have a way to reference the webview instance of the last activity open and, if exists, finish the webview streaming before starting a new one... any idea? Help!
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Have you tried removing the webview from your activity in the activity_pause, and re-adding it in your activity_resume?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Have you tried removing the webview from your activity in the activity_pause, and re-adding it in your activity_resume?
I don't want to remove because, as I said, the background streaming activity is interesting for the times when the user wants to navigate to other activity or app maintaining a music running, for example. The problem is that the B4A triggers again the event Activity_Create after user pressed back button, for example... if the streaming was destroyed also, no problem. But the finish looks like a "partial finish" because the streaming process linked to the destroyed webview is still running - this is when the problem starts: the system will try to create other websocket which conflicts with the first one.
I did a workaround in this way:
1. Blocked back button
2. Created two buttons on screen:
- one to destroy the streaming BEFORE destroying the activity, loading a blank html page on webview (before finishing the activity, I still have the reference for its instance)
- other only starting other activity (the new one that the user wants to run), without finishing the current - so the audio stream remains... and isn't loaded again because when the user returns to the webview activity there is only a Resume event, not a Create...

This is my solution BUT I really would like to be able to destroy the streaming object attached to the webview - this should be the "perfect solution".

Thanks!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
You can force the WebView to be "destroyed" with:
B4X:
Dim jo As JavaObject = WebView1
jo.RunMethod("destroy", Null)

Call it in Activity_Pause and make sure to create a new WebView in Activity_Resume.
Thanks @Erel . Like I told to @Jeffrey Cameron , it's interesting to have the streaming working in background when the user navigates to other activities (and the streaming is generated dynamically by site, then it's not possible to connect directly). Then, could be very useful if I could:

1. Check if the webview instace is active - then, I could to test in _Resume and avoid to load one more if is still running and...
2. Destroy the instance outside the parent activity: maintaining, for example, a floating window (overlay window) where user could stop the streaming... this window could be showed when parent activity is paused and closed when opened or the webview instance is closed.

To do this, I need to be able to reference the webview outside the parent activity and check if it's running or not. Referencing that with javaobject could be useful if could be done in this way:
1. Create webview dinamically from Process_Global (then, the object will be valid in the entire app process and will be unique
2. Load and unload in activity dynamically also...

I'm not sure if you understood but I think that this is the best solution... any suggestion?
 
Upvote 0
Top