Nativescript webview和android后退按钮 [英] Nativescript webview and android back button

查看:136
本文介绍了Nativescript webview和android后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nativescript中创建了一个只包含webview的页面。这是我的代码:

I have a made a page in nativescript that only containes a webview. Here is my code:

<Page xmlns="http://www.nativescript.org/tns.xsd" actionBarHidden="true">

    <WebView src="http://example.com/" />

</Page>

这是JS:

import * as webViewModule from "tns-core-modules/ui/web-view";

let webView = new webViewModule.WebView();

webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) {
    let message;
    if (!args.error) {
        message = "WebView finished loading " + args.url;
    }
    else {
        message = "Error loading " + args.url + ": " + args.error;
    }

});
webView.src = "http://example.com/";

在按下android后退按钮之前,一切正常。然后在导航到webview内的最后一页时,应用程序就存在了。
当我从应用程序菜单再次打开它(返回到相同的最小化应用程序)时,它会重新加载webview内容而不保存它的状态。

Everything is working great until I press the android back button. Then inside of navigating to the last page inside the webview, the app just exists. When I open it again from the applications menu (returning to the same minimized app) it reloads the webview content and not saving it's state.

帮助将是赞赏。

推荐答案

你需要像这样处理Android硬按钮:

You need to handle Android hard button like this:

首先导入依赖项:

import { AndroidApplication, AndroidActivityBackPressedEventData } from "application";
import * as application from "application";

然后添加以下代码:

application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
  data.cancel = true; // prevents default back button behavior
  console.log("webview can go back "+this.webView.canGoBack);
  if (webView.canGoBack) //if webview can go back
      webView.goBack();
  else
      this.router.backToPreviousPage();
});

这篇关于Nativescript webview和android后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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