Does WebView not seem to be destroyed after leaving the business?

I have activity with web browsing. It seems that web browsing does not collapse along with activity. To demonstrate, I created an html page that starts a timer that loads an image resource every few seconds. The script continues execution after the activity is destroyed:

public class MyWebViewActivity extends Activity { private WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.the_layout); mWebView = (WebView)findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.setWebViewClient(new EmbeddedWebViewClient()); mWebView.loadUrl("test url"); } private class EmbeddedWebViewClient extends WebViewClient { @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); if (Config.DEBUG) { Log.v(TAG, "onLoadResource(): " + url); } } } } 

So the above just prints a log statement whenever onLoadResource () is called. Here's the javascript on the html test page:

 <html> <head> <script type="text/javascript"> function load() { setInterval("doit()", 3000); } function doit() { Image1 = new Image(150, 20); Image1.src = "abc_" + Math.floor(Math.random()*100) + ".png"; } </script> </head> <body onload="load()"> </body> </html> 

The above simply creates an image resource on a timer interval to run the onLoadResource () method in EmbeddedWebViewClient ().

So, I see in LogCat that messages continue to print after exiting this activity. Do we need to close the WebView somehow? Perhaps this is the problem described here:

http://code.google.com/p/android/issues/detail?id=9375

If this is a known issue, if it has been documented in api docs?

Note. Doing this on Android 2.3.4, Nexus S.

thanks

+8
android
source share
4 answers

If the action does not resume, you can also call mWebView.stopLoading ().

+1
source share

I am not familiar with WebView, but are you sure that your activity is really collapsing? Try to call

 this.finish() 

when you want him to go out. If this still does not work, you want to gain control over what happens when the activity is about to die:

 @Override protected void onDestroy() { // make a call to your script, tell it to end } 

Of course, if you want your script to stop without completely destroying your activity, you can override onPause () or onStop ()

Hope this helps

0
source share

You are probably looking for pauseTimers () and resumeTimers () calls. If you add this to your activity, I think that he will start to do what you expect:

 public void onResume() { super.onResume(); if (mWebView != null) { mWebView.resumeTimers(); } } public void onPause() { super.onPause(); if (mWebView != null) { mWebView.pauseTimers(); } } 

Logcat messages stop when you switch from the application and start the backup on return.

0
source share

It seems not.

android develop doc here says

The standard behavior for an Activity must be destroyed and recreated when the device orientation or any other configuration changes. This will force WebView to reload the current page.

So, I think that there is no need to destroy the web view when you leave it. Just listen to some event.

0
source share

All Articles