NativeScript处理后退按钮事件 [英] NativeScript handling back button event

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

问题描述

我正在尝试处理NativeScript应用程序中的硬件后退按钮.我在Angular中使用NativeScript 2.3.0版.

I am trying to handle the hardware back button in a NativeScript app. I am using NativeScript version 2.3.0 with Angular.

这是main.ts文件中的内容

// this import should be first in order to load some required settings (like globals and reflect-metadata)
import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
import { NgModule,Component,enableProdMode } from "@angular/core";
import { AppComponent } from "./app.component";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { routes, navigatableComponents } from "./app.routing";
import { secondComponent } from "./second.component";
import {AndroidApplication} from "application";
@Component({
    selector: 'page-navigation-test',
    template: `<page-router-outlet></page-router-outlet>`
})
export class PageNavigationApp {
}
@NgModule({
    declarations: [AppComponent,PageNavigationApp,secondComponent
        // ...navigatableComponents
        ],
    bootstrap: [PageNavigationApp],
    providers:[AndroidApplication],
    imports: [NativeScriptModule,
        NativeScriptRouterModule,
    NativeScriptRouterModule.forRoot(routes)
    ],
})
class AppComponentModule {

    constructor(private androidapplication:AndroidApplication){
        this.androidapplication.on("activityBackPressed",()=>{
            console.log("back pressed");

        })
    }

}

enableProdMode();

platformNativeScriptDynamic().bootstrapModule(AppComponentModule);

我正在使用

import {AndroidApplication} from "application";

然后在appComponentModule的构造器中,我为activityBackPressed注册该事件,并只是执行console.log.

Then in the constrouctor of appComponentModule I am registering the event for activityBackPressed and just doing a console.log.

这不起作用.

我在这里想念什么?

推荐答案

我也在Angular中使用NativeScript,这对我来说似乎很好:

I'm using NativeScript with Angular as well and this seems to work quite nicely for me:

import { RouterExtensions } from "nativescript-angular";
import * as application from "tns-core-modules/application";
import { AndroidApplication, AndroidActivityBackPressedEventData } from "tns-core-modules/application";
import { isAndroid } from "tns-core-modules/platform";

export class HomeComponent implements OnInit {
  constructor(private router: Router) {}

  ngOnInit() {
    if (!isAndroid) {
      return;
    }
    application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
      if (this.router.isActive("/articles", false)) {
        data.cancel = true; // prevents default back button behavior
        this.logout();
      }
    });
  }
}

请注意,挂在backPressedEvent上是一个全局问题,因此,根据上述示例,您需要检查您所在的页面并采取相应的措施.

Note that hooking into the backPressedEvent is a global thingy so you'll need to check the page you're on and act accordingly, per the example above.

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

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