我在NativeScript中有一个仅包含WebView的页面。这是我的代码:
<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内容而不是保存其状态。
将不胜感激。
您需要像这样处理Android硬按钮:
第一个导入依赖项:
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 " + webView.canGoBack);
if (webView.canGoBack) //if webview can go back
webView.goBack();
else
this.router.backToPreviousPage();
});