最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nativescript webview and android back button - Stack Overflow

programmeradmin6浏览0评论

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

<Page xmlns=".xsd" actionBarHidden="true">

    <WebView src="/" />

</Page>

And here is the 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 = "/";

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.

Help will be appreciated.

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

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

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

</Page>

And here is the 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./";

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.

Help will be appreciated.

Share Improve this question asked Feb 6, 2018 at 15:56 Amit AisikowitzAmit Aisikowitz 1432 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You need to handle Android hard button like this:

First import dependencies :

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

Then add this code :

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();
});
发布评论

评论列表(0)

  1. 暂无评论