feat: Implement back button handling using OnBackPressedDispatcher
- Replaced deprecated onBackPressed() method with OnBackPressedDispatcher for better handling of back navigation in WebView. - Updated WebViewActivity to inject JavaScript to prevent PWA installation prompts. - Improved file chooser functionality with ActivityResultLauncher. - Enhanced user experience with Snackbar for error messages.
This commit is contained in:
parent
8fe640ef2b
commit
eddb5ed553
1 changed files with 38 additions and 31 deletions
|
|
@ -6,10 +6,12 @@ import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.webkit.*
|
import android.webkit.*
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import androidx.activity.OnBackPressedCallback
|
||||||
|
import androidx.activity.result.ActivityResult
|
||||||
|
|
||||||
class WebViewActivity : AppCompatActivity() {
|
class WebViewActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
|
@ -30,16 +32,7 @@ class WebViewActivity : AppCompatActivity() {
|
||||||
|
|
||||||
// 初始化文件选择器启动器
|
// 初始化文件选择器启动器
|
||||||
fileChooserLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
fileChooserLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
|
handleFileChooserResult(result)
|
||||||
val resultUri = result.data?.data
|
|
||||||
if (filePathCallback != null) {
|
|
||||||
filePathCallback?.onReceiveValue(resultUri?.let { arrayOf(it) })
|
|
||||||
filePathCallback = null
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
filePathCallback?.onReceiveValue(null)
|
|
||||||
filePathCallback = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取传递的 URL
|
// 获取传递的 URL
|
||||||
|
|
@ -49,6 +42,18 @@ class WebViewActivity : AppCompatActivity() {
|
||||||
} else {
|
} else {
|
||||||
showSnackbar("No URL provided")
|
showSnackbar("No URL provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理返回键事件
|
||||||
|
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
|
||||||
|
override fun handleOnBackPressed() {
|
||||||
|
if (webView.canGoBack()) {
|
||||||
|
webView.goBack()
|
||||||
|
} else {
|
||||||
|
isEnabled = false // Disable the callback so that the default behavior can occur
|
||||||
|
finish() // Close the activity when there's no page to go back to
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupWebView() {
|
private fun setupWebView() {
|
||||||
|
|
@ -63,9 +68,19 @@ class WebViewActivity : AppCompatActivity() {
|
||||||
|
|
||||||
override fun onPageFinished(view: WebView?, url: String?) {
|
override fun onPageFinished(view: WebView?, url: String?) {
|
||||||
super.onPageFinished(view, url)
|
super.onPageFinished(view, url)
|
||||||
// 注入 JavaScript 来禁用 PWA 安装提示
|
|
||||||
injectJavaScript(view)
|
injectJavaScript(view)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun injectJavaScript(view: WebView?) {
|
||||||
|
view?.evaluateJavascript(
|
||||||
|
"""
|
||||||
|
window.addEventListener('beforeinstallprompt', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
console.log('Install prompt blocked by WebView');
|
||||||
|
});
|
||||||
|
""".trimIndent(), null
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
webView.webChromeClient = object : WebChromeClient() {
|
webView.webChromeClient = object : WebChromeClient() {
|
||||||
|
|
@ -87,17 +102,6 @@ class WebViewActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun injectJavaScript(view: WebView?) {
|
|
||||||
view?.evaluateJavascript(
|
|
||||||
"""
|
|
||||||
window.addEventListener('beforeinstallprompt', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
console.log('Install prompt blocked by WebView');
|
|
||||||
});
|
|
||||||
""".trimIndent(), null
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun launchFileChooser() {
|
private fun launchFileChooser() {
|
||||||
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
|
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
|
|
@ -106,15 +110,18 @@ class WebViewActivity : AppCompatActivity() {
|
||||||
fileChooserLauncher.launch(Intent.createChooser(intent, "Select Picture"))
|
fileChooserLauncher.launch(Intent.createChooser(intent, "Select Picture"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun handleFileChooserResult(result: ActivityResult) {
|
||||||
|
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
|
||||||
|
val resultUri = result.data?.data
|
||||||
|
filePathCallback?.onReceiveValue(resultUri?.let { arrayOf(it) })
|
||||||
|
filePathCallback = null
|
||||||
|
} else {
|
||||||
|
filePathCallback?.onReceiveValue(null)
|
||||||
|
filePathCallback = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun showSnackbar(message: String) {
|
private fun showSnackbar(message: String) {
|
||||||
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show()
|
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
|
||||||
if (webView.canGoBack()) {
|
|
||||||
webView.goBack()
|
|
||||||
} else {
|
|
||||||
super.onBackPressed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue