36

I am getting this error when the device changes orientation:

Error: WebView.destroy() called while still attached

With this code:

protected void onDestroy()
{
    if (adView != null)
    {
        adView.destroy();
    }
}

What is the reason for this? How do I avoid this error?

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
hellowill89
  • 1,538
  • 2
  • 15
  • 26
  • 1
    maybe the whole call stack will help – nandeesh Aug 16 '12 at 22:27
  • 3
    Before calling destroy you need to remove the WebView from the views system See http://developer.android.com/reference/android/webkit/WebView.html#destroy() Thanks – ebtokyo Aug 22 '12 at 01:12

5 Answers5

54

You first need to detach the Webview:

webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();

That did it for me.

user1668939
  • 564
  • 4
  • 3
31

To avoid the error you just need to remove all views before you destroy the ad.

@Override
public void onDestroy()
{
    if (adView != null)
    {
        adView.removeAllViews();
        adView.destroy();
    }
    super.onDestroy();
}
Matthew
  • 3,411
  • 2
  • 28
  • 28
  • 3
    I don't wanna disappoint you, but this doesn't work. I have the same problem with my WebView and I always close my WebView like this and the error exists nevertheless. – Leandros Sep 08 '12 at 17:56
  • 2
    This works for me. The official admob doc should add adView.removeAllViews(); I don't want the ad still consumes resources after the activity is destroyed. – Vince Yuan Jul 08 '13 at 04:09
  • 1
    It don't solve the problem completely. I also meet it, app still crash although use this code – BaDo Feb 28 '14 at 09:07
10
 @Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (mWebView != null) {
        mWebView.destroy();
    }
}
Meng
  • 181
  • 2
  • 6
8

According to my tests, this issue is revealed in AdMob SDK v6.4.1 and at least on Android v4.2.2+. When testing the AdMob sample application referred to at https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android (direct link is http://google-mobile-dev.googlecode.com/files/Android_XML.zip), the issue occurs when closing the sample screen.

My work-around is rather:

 @Override
  public void onDestroy()
  {
    // Destroy the AdView.
    if (adView != null)
    {
      final ViewGroup viewGroup = (ViewGroup) adView.getParent();
      if (viewGroup != null)
      {
        viewGroup.removeView(adView);
      }
      adView.destroy();
    }

    super.onDestroy();
  }

Hope that helps other people, and that the AdMob will very soon fix that annoying issue.

Édouard Mercier
  • 485
  • 5
  • 12
7

For you don't get this error you need to have a parent layout, e.g. : RelativeLayout and remove the WebView component, that might had been defined on you layoutWebView.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webviewRelativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/headerAlarmsWebViewTxt"
    android:layout_marginBottom="0dip"
    android:hapticFeedbackEnabled="true"
    android:overScrollMode="never"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:scrollbars="none" />

 </RelativeLayout>

Then you assign it to an instance variable e.g. :

_layout = (RelativeLayout) findViewById(R.id.webviewRelativeLayout);
webView = (WebView) findViewById(R.id.webView1);

and on Destroy do something like this:

@Override
protected void onDestroy() {
    super.onDestroy();
    _layout.removeView(webView);
    webView.setFocusable(true);
    webView.removeAllViews();
    webView.clearHistory();
    webView.destroy();
}
Klaus Villaca
  • 587
  • 1
  • 6
  • 11