android - 在 Activity 之间共享 WebView

标签 android android-webview

我写了一个 iOS 应用程序,我想移植到 android。此应用程序是工作中基于网络的计时系统的前端。不幸的是,该系统没有 API,因此我在 javascript 中使用 xpath 查询对其进行屏幕抓取。在 iOS 中,我只需加载此页面一次,因为我可以完全控制 UIWebView 实例何时被销毁。

iOS 应用只有 3 个用例,每个都分解为单独的 Android Activity :

  • 登录
  • 查看所有报告时间的列表
  • 报告新时间。

使用天真的方法,当我在 View 之间切换时,我的 android View (包括我需要用来与计时系统交互的 WebView)将被销毁并重新创建。如果我每次切换 Activity 时都必须重新加载 WebView,我的应用程序会显得很慢。

是否可以在多个之间共享一个 WebView 实例 Activity ?我知道我可以将 launchMode 设置为 singleInstance,但理想情况下,最好允许单独的实例以便后退按钮正常工作。

最佳答案

根据 suggestion on from Eric Burke at STL Mobile Dev :

See if your Android Application can create the WebView. You won't actually display the WebView anywhere, it's just created for interaction with the web site. The WebView instance's lifecycle is then managed by your Application rather than an Activity.

我能够在我的 Activity 实例之间共享我的 WebView。我在 list 中指定了自定义 Application,并在 Application 中创建了 WebViewApplication 是一个 Context,它与应用程序一样存在,因此所有 Activity 对象都可以共享 WebView 那样。

关于android - 在 Activity 之间共享 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988698/

相关文章:

android - 使用 Android 数据绑定(bind)将列表类型导入 xml 文件时出错

android - 使用 Glide 库设置完成图像加载后进度条的可见性

android 自定义 Intent 选择器

android - 是否可以访问 WebView 缓存?

android - 哪个站点显示在应用程序的 WebView 中

android - flutter_inappwebview 和 flutter_downloader - 不知道我做错了什么?

android - 使用 Spinner Android 对自定义 ListView 项目进行排序

android - 如何重复 fragment (如循环)?

android - 如何让Flowplayer在Android WebView中自动播放?

javascript - Android Webview HTML 加载 Javascript 问题