开发者

WebView doesn't seem to get destroyed after leaving activity?

开发者 https://www.devze.com 2023-03-15 15:28 出处:网络
I\'ve got an activity with a webview. Looks like the webview does not get destroyed along with the activity. To demonstrate, I\'ve created an html page which runs a timer that loads an image resource

I've got an activity with a webview. Looks like the webview does not get destroyed along with the activity. To demonstrate, I've created an html page which runs a timer that loads an image resource every few seconds. The script continues executing 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().setBuiltInZ开发者_开发百科oomControls(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 in the test html 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 just creates an image resource on the timer interval to trigger the onLoadResource() method in EmbeddedWebViewClient().

So yeah I can see in LogCat that the messages keep printing after leaving the activity. Do we have to shut down the WebView somehow? Maybe this is the issue described here:

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

if it's a known issue should it have been documented in the api docs?

Note: Running this on android 2.3.4, Nexus S.

Thanks


This solution worked for me:

In MyActivity.java file, I added:

@Override
protected void onDestroy() {
    webview.destroy();
    webview = null;
    super.onDestroy();
}


You're probably looking for the pauseTimers() and resumeTimers() calls. If you add this to your activity I think it'll start doing what you expect:

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

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

The logcat messages stop when you swap away from the app and start back up when you return.


If the activity will not be resumed, you can also call mWebView.stopLoading().


i'm not familiar with WebView but are you sure your activity is really getting destroyed? Try calling

this.finish()

when you want it to exit. If that still doesn't work, then 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


It seems not.

android develop doc here says

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

So, I think there is no need to destroy the webview when you leave the activity. Just listen some event.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号