WebClient

open class WebClient(allowedHosts: List<String?>) : WebViewClient, DownloadListener

A web client that solves many problems of WebViewClient and provides a nice API to use. This client automatically sets JS as enabled. You can override this behavior by calling WebSettings.setJavaScriptEnabled. Only domains in allowedHosts will be opened in the webview, others will trigger WebClientListener.onForeignUrlEncountered. Don't forget to attach the client in onViewCreated and detach it in onDestroyView.

Constructors

Link copied to clipboard
constructor(allowedHosts: List<String?>)

Properties

Link copied to clipboard
open val canGoBack: Boolean

Whether the web history is not empty and the user can go back

Link copied to clipboard
val url: String?

Current webview URL, or null if nothing is loaded

Functions

Link copied to clipboard
open fun attach(webView: WebView, listener: WebClientListener? = null, userAgent: String? = null, javaScriptEnabled: Boolean = true): WebClient

Call this in androidx.fragment.app.Fragment.onViewCreated

Link copied to clipboard
open fun clearAllData(forAllWebViews: Boolean = false)

Clear all data associated with web views on device. Clears cookies and localstorage as well if forAllWebViews is true

Link copied to clipboard
open fun clearHistory()

Clear the navigation history

Link copied to clipboard
open fun detach()

Call this in androidx.fragment.app.Fragment.onDestroyView. If you do not call this, you will get crashes in runtime and a memory leak!

Link copied to clipboard
Link copied to clipboard
open fun goBack()

Go back through the browser history if possible

Link copied to clipboard
open fun load(uri: Uri)

Load an uri

Link copied to clipboard
open override fun onDownloadStart(url: String?, userAgent: String?, contentDisposition: String?, mimetype: String?, contentLength: Long)
Link copied to clipboard
open fun onFormResubmission(p0: WebView, p1: Message, p2: Message)
Link copied to clipboard
open fun onLoadResource(p0: WebView, p1: String)
Link copied to clipboard
Link copied to clipboard
open override fun onPageFinished(view: WebView?, url: String?)
Link copied to clipboard
open override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?)
Link copied to clipboard
open fun onReceivedError(p0: WebView, p1: Int, p2: String, p3: String)
open override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun onReceivedLoginRequest(p0: WebView, p1: String, p2: String?, p3: String)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun onScaleChanged(p0: WebView, p1: Float, p2: Float)
Link copied to clipboard
open fun onTooManyRedirects(p0: WebView, p1: Message, p2: Message)
Link copied to clipboard
Link copied to clipboard
open fun reload()

Refresh current page.

Link copied to clipboard
open fun restoreState(inState: Bundle)

Call this in androidx.appcompat.app.AppCompatActivity.onSaveInstanceState state or androidx.fragment.app.Fragment.onViewStateRestored, if you do not save state, webView will not save it for you!

Link copied to clipboard
open fun saveState(outState: Bundle)

Call this in androidx.fragment.app.Fragment.onSaveInstanceState to save webview state. If you do not call this, webview won't save it for you!

Link copied to clipboard
Link copied to clipboard