Android WebView 防止页面重新加载 [英] Android WebView prevent page reload

查看:62
本文介绍了Android WebView 防止页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Phonegap 打包应用程序,它在按下按钮时打开一个 PDF 查看器.我是用 FileOpener2 插件做到的.

I have a Phonegap packaged application that opens a PDF Viewer when a button is pressed. I did it with the FileOpener2 plugin.

当我从 PDF 查看器返回应用程序时,结果因 Android 版本而异.

When I go back to the application from the PDF Viewer, the result differs depending on Android versions.

在 Android 4.* 中,应用程序在离开应用程序之前会停留在它应该在的位置.但是,在 Android 2.3.7 中,WebView 会重新加载应用程序,这是我不期望的行为.

In Android 4.* the application stays where it should be before leaving the application. However, in Android 2.3.7 the WebView reloads the application, which is the behaviour I don´t expect.

我已经尝试将 android:launchmode 更改为 singleTask 或 singleInstance,但没有成功.

I already tried changing android:launchmode to singleTask or singleInstance with no success.

当我回到 Phonegap 应用程序时如何避免页面重新加载?

How can I avoid page reload when I came back to the Phonegap application?

提前致谢!

更新 1

我修改了 Cordova 或 Phonegap 类 CordovaWebViewClient,它扩展了 WebViewClient:

I have modified the Cordova or Phonegap class CordovaWebViewClient which extends WebViewClient with:

@Override
public void onLoadResource(WebView view, String url) {
    if (url.equals("file:///android_asset/www/index.html")) {
        if (this.firstLoad) {
            this.firstLoad = false;
        } else {
            view.stopLoading();
        }
    }

但是 onLoadResource 仅在应用程序启动时触发,而不是从另一个应用程序恢复时触发.

However the onLoadResource only gets fired when the application started and not when it resumes from another application.

推荐答案

回答我自己的问题,将以下属性设置为AndroidManifest中唯一的Acivity即可解决问题.但是我必须对其进行测试,因为它可能存在我不知道的问题:

Answering my own question, setting the following properties to the only Acivity in AndroidManifest solves the problem. However I have to test it because it could have problems that I don't know:

android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"

干杯!

更新

在测试出现错误后,使用 Phonegap 相机插件在图库中选择一张照片,显示选择已取消".

After testing a bug appeared selection a photo in the Gallery with Phonegap Camera plugin that shows "Selection cancelled".

所以在阅读https://groups.google.com/forum/#!topic/phonegap/R08vOZNm580 我设置:

 android:launchMode="singleTask"
 android:taskAffinity=""
 android:excludeFromRecents="true"

这篇关于Android WebView 防止页面重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆