delight-im / Android-AdvancedWebView

Enhanced WebView component for Android that works as intended out of the box
MIT License
2.39k stars 574 forks source link

Out of the box: Listener methods #7

Closed jflamman closed 9 years ago

jflamman commented 9 years ago

Following the steps in the readme, Android Studio still threw warnings the Activity must be either abstract or implement AdvancedWebView's Listener methods.

It stopped complaining when I copied the methods from mWebView.setListener to the Activity class and commented out the mWebView.setListener part in onCreate altogether.

@Override
public void onPageStarted(String url, Bitmap favicon) {
    // a new page started loading
}

@Override
public void onPageFinished(String url) {
    // the new page finished loading
}

@Override
public void onPageError(int errorCode, String description, String failingUrl) {
    // the new page failed to load
}

@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
    // some file is available for download
}

@Override
public void onExternalPageRequest(String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    mWebView.getContext().startActivity(intent);
}

(I added the Intent to onExternalPageRequest to be able to have external links open in my phone's browser)

ocram commented 9 years ago

Thanks!

This was actually intended behaviour, as there are two ways to implement the listener:

You don't need both ways, of course.

Adding the methods to the Activity or Fragment class was correct. And then you can remove the anonymous class from setListener() and just call it with this, this.

Does that help?

The documentation was poor, you're right. Has been fixed with https://github.com/delight-im/Android-AdvancedWebView/commit/8252c4e3808f5b31c9c1dbf37b0cd31e6472ccb5

jflamman commented 9 years ago

OK, that helps! I'm not a Java geek and I wasn't sure my workaround was a hack or an allowed way to make things work. :-)

mWebView.setListener(this, this); is already there, so I guess that takes care of it then.

Thank you for all your work & your quick responses..

ocram commented 9 years ago

Great! That's fine, now things have been clarified :)

The README was definitely confusing before. Thanks for pointing out!

jflamman commented 9 years ago

Glad to be of help, keep up the good work!