Created
October 29, 2018 11:02
-
-
Save undownding/3fe4cfd5af3a92bb0cdaa713655015f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.undownding.common | |
import android.annotation.SuppressLint | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.view.MenuItem | |
import android.webkit.WebView | |
import android.webkit.WebViewClient | |
class WebViewActivity: AppCompatActivity() { | |
private val webView by lazy { WebView(this) } | |
@SuppressLint("SetJavaScriptEnabled") | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(webView) | |
supportActionBar?.setDisplayHomeAsUpEnabled(true) | |
with(webView.settings) { | |
javaScriptEnabled = true | |
domStorageEnabled = true | |
databaseEnabled = true | |
} | |
webView.webViewClient = object : WebViewClient() { | |
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { | |
view?.loadUrl(url) | |
return true | |
} | |
override fun onPageFinished(view: WebView?, url: String?) { | |
supportActionBar?.title = view?.title | |
} | |
} | |
webView.loadUrl(intent.dataString) | |
} | |
override fun onOptionsItemSelected(item: MenuItem?): Boolean = when(item?.itemId) { | |
android.R.id.home -> { | |
onBackPressed() | |
true | |
} | |
else -> { | |
super.onOptionsItemSelected(item) | |
} | |
} | |
override fun onBackPressed() { | |
if (webView.canGoBack()) { | |
webView.goBack() | |
} else { | |
finish() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment