Reputation: 1307
I have a webview with an audio player. If I start the audio and press back to return to the previous activity, the audio still keeps playing in background. This problem only occurs when the API is lower than 11.
My code:
@Override
public void finish() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
webView.onPause();
} else {
webView.loadUrl("about:blank");
}
}
I also tried
webView.loadData("", "text/html", "utf-8");
And
webView.clearCache(true);
webView.clearHistory();
webView.destroy();
But it didn't work.
Does anyone know how to solve that?
Upvotes: 1
Views: 2770
Reputation: 477
A simple hack that worked for me is
webView.loadUrl("javascript:document.location=document.location");
Upvotes: 2
Reputation: 2258
This solves the problem fro API > 11
@Override
public void onPause() {
super.onPause();
if(webView != null) {
webView.onPause();
}
}
@Override
public void onResume() {
super.onResume();
if (webView != null) {
webView.onResume();
}
}
but for lower versions (in your case) you need to call onPause
and onResume
as follows
@Override
public void onPause() {
super.onPause();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
webview.onPause();
} else {
try {
Class.forName("android.webkit.WebView")
.getMethod("onPause", (Class[]) null)
.invoke(webview, (Object[]) null);
} catch(ClassNotFoundException cnfe) {
...
} catch(NoSuchMethodException nsme) {
...
} catch(InvocationTargetException ite) {
...
} catch (IllegalAccessException iae) {
...
}
}
}
@Override
public void onResume() {
super.onPause();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
webview.onResume();
} else {
try {
Class.forName("android.webkit.WebView")
.getMethod("onResume", (Class[]) null)
.invoke(webview, (Object[]) null);
} catch(ClassNotFoundException cnfe) {
...
} catch(NoSuchMethodException nsme) {
...
} catch(InvocationTargetException ite) {
...
} catch (IllegalAccessException iae) {
...
}
}
}
Upvotes: 0