Android Back Button Multiple Events Handling

nativescriptcore

#1

Hi
This is regarding handling of AndroidApplication.activityBackPressedEvent. It is obvious that we may want to handle the way back button works differently based on page to page. The events added on the back page stay alive even when we move to another page where we want to add a different functionality for back button.
To make it clear, I have created a playground example for android https://play.nativescript.org/?template=play-tsc&id=oyM8O7&v=8.
Steps: Move across the pages by clicking on the buttons and then press back button on android. You would see multiple dialogs appearing from functions we added on different typescript pages.

How can we remove these functions which we added on previous pages?

Thanks,
Rakesh


#2

A Page !== Activity, back button event can be monitored only on a Activity level not at Page level. But if you want to monitor it efficiently on page level, you may do something like this.


#3

The most important bit is app.ts where we listen to activityBackPressedEvent and redirect the event to appropriate page handler.