1
1
package ru.voko.oracleplsql
2
2
3
- import androidx.appcompat.app.AppCompatActivity
3
+ import android.annotation.SuppressLint
4
+ import android.content.Intent
5
+ import android.net.Uri
4
6
import android.os.Bundle
7
+ import android.view.KeyEvent
8
+ import android.webkit.WebResourceRequest
9
+ import android.webkit.WebView
10
+ import android.webkit.WebViewClient
11
+ import androidx.appcompat.app.AppCompatActivity
12
+ import ru.voko.oracleplsql.databinding.ActivityMainBinding
5
13
6
14
class MainActivity : AppCompatActivity () {
15
+
16
+ private lateinit var binding: ActivityMainBinding
17
+
7
18
override fun onCreate (savedInstanceState : Bundle ? ) {
8
19
super .onCreate(savedInstanceState)
9
- setContentView(R .layout.activity_main)
20
+ binding = ActivityMainBinding .inflate(layoutInflater)
21
+ setContentView(binding.root)
22
+
23
+ binding.webView.webViewClient = MyWebViewClient ()
24
+
25
+ val webSettings = binding.webView.settings
26
+ @SuppressLint(" SetJavaScriptEnabled" )
27
+ webSettings.javaScriptEnabled = true
28
+
29
+ if (savedInstanceState == null ) {
30
+ binding.webView.loadUrl(" file:///android_asset/index.html" )
31
+ }
32
+ }
33
+
34
+ override fun onSaveInstanceState (outState : Bundle ) {
35
+ super .onSaveInstanceState(outState)
36
+ binding.webView.saveState(outState)
10
37
}
11
- }
38
+
39
+ override fun onRestoreInstanceState (savedInstanceState : Bundle ) {
40
+ super .onRestoreInstanceState(savedInstanceState)
41
+ binding.webView.restoreState(savedInstanceState)
42
+ }
43
+
44
+ override fun onKeyDown (keyCode : Int , event : KeyEvent ): Boolean {
45
+ if (keyCode == KeyEvent .KEYCODE_BACK && binding.webView.canGoBack()) {
46
+ binding.webView.goBack()
47
+ return true
48
+ }
49
+ return super .onKeyDown(keyCode, event)
50
+ }
51
+
52
+ private inner class MyWebViewClient : WebViewClient () {
53
+
54
+ override fun shouldOverrideUrlLoading (
55
+ view : WebView ? ,
56
+ request : WebResourceRequest ?
57
+ ): Boolean {
58
+ if (Uri .parse(request?.url.toString()).host
59
+ == Uri .parse(" file:///android_asset/index.html" ).host
60
+ ) {
61
+ return false
62
+ }
63
+ Intent (Intent .ACTION_VIEW , Uri .parse(request?.url.toString())).apply {
64
+ startActivity(this )
65
+ }
66
+ return true
67
+ }
68
+ }
69
+
70
+ }
0 commit comments