我写了一个 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 theWebView
. You won't actually display theWebView
anywhere, it's just created for interaction with the web site. TheWebView
instance's lifecycle is then managed by yourApplication
rather than anActivity
.
我能够在我的 Activity
实例之间共享我的 WebView
。我在 list 中指定了自定义 Application
,并在 Application
中创建了 WebView
。 Application
是一个 Context
,它与应用程序一样存在,因此所有 Activity
对象都可以共享 WebView
那样。
关于android - 在 Activity 之间共享 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988698/