Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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().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 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

like image 303
user291701 Avatar asked Jun 27 '11 22:06

user291701


People also ask

How do I exit Webview?

If you have only 1 activity you can simply start the service and then call finish() on the activity. However, if you have multiple activities you have to make sure that you close them all (see also this post).

How do I close Webview on Android?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.

When the back button is pressed in Webview the app exit?

Bookmark this question. Show activity on this post. Instead of going back to the previous page, the app is closed when pressed the back button...

Is Android Webview deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


2 Answers

This solution worked for me:

In MyActivity.java file, I added:

@Override
protected void onDestroy() {
    webview.destroy();
    webview = null;
    super.onDestroy();
}
like image 111
Maybelle Pacate Avatar answered Oct 04 '22 14:10

Maybelle Pacate


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.

like image 41
mikerowehl Avatar answered Oct 04 '22 16:10

mikerowehl