Nativescript angular:处理不同页面上的android后退按钮 [英] Nativescript angular : handle android back button on different pages
问题描述
因此,我使用此功能来处理android后退按钮:
So I use this function to handle android back button :
this._page.on(Page.loadedEvent, event => {
if (application.android) {
application.android.on(application.AndroidApplication.activityBackPressedEvent, (args:AndroidActivityBackPressedEventData) => {
args.cancel = true;
this._ngZone.run(() => {
this.router.navigate(['/parameters']);
});
});
}
})
在不同页面上(角组件).因此,在page1.ts上,我具有navigation(['/parameters]),在page2.ts上,我具有console.log("test").问题是无论我在应用程序中的哪个位置,如果我在右侧页面上,请始终按向后按钮进行导航(['//parameters]),也可以进行console.log,但是应该只进行console.log 强>. 似乎是全局的,有什么主意如何覆盖activityBackPressedEvent吗?
on different pages (angular components). So on page1.ts I have navigate(['/parameters]) and on page2.ts I have console.log("test"). Problem is wherever I am in the app, pressing back button always do navigate(['/parameters]), also the console.log if i'm on the right page, but it should do console.log only. It seems to be global, any idea how to override activityBackPressedEvent ?
推荐答案
activityBackPressedEvent
并非特定于页面,它对于保存所有页面的Activity是全局的.通常,您不会向该事件添加多个事件侦听器.
activityBackPressedEvent
is not specific to a page, it's global to your Activity which holds all the pages. Generally, You will not add more than one event listener to this event.
您可以在应用程序模块/main.ts
You could do something like below to handle this on page level, probably in app module / main.ts
application.android.on(application.AndroidApplication.activityBackPressedEvent,
(args: application.AndroidActivityBackPressedEventData) => {
const page = frame.topmost().currentPage;
if (page.hasListeners(application.AndroidApplication.activityBackPressedEvent)) {
args.cancel = true;
page.notify({
eventName: application.AndroidApplication.activityBackPressedEvent,
object: page
});
}
});
使用上述代码,将在具有侦听器的每个页面上触发activityBackPressedEvent
.
With above code, activityBackPressedEvent
willl be triggered on every page that has a listener.
现在要在页面/组件中自定义行为,
Now in your page / component in which you want to customise the behaviour you do this,
// Inject Page
constructor(private page: Page) {
this.page.on(application.AndroidApplication.activityBackPressedEvent, this.onBackButtonTap, this);
}
onBackButtonTap(data: EventData) {
this._ngZone.run(() => {
this.router.navigate(['/parameters']);
});
}
这篇关于Nativescript angular:处理不同页面上的android后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!