NativeScript handling back button event

后端 未结 4 482
别跟我提以往
别跟我提以往 2020-12-16 06:03

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

Here is what I have in main.ts

4条回答
  •  囚心锁ツ
    2020-12-16 06:49

    As far as I know, NativeScript has a built-in support for this but it's not documented at all. Using onBackPressed callback, you can handle back button behaviour for View components (e.g. Frame, Page, BottomNavigation).

    Example:

    function pageLoaded(args) {
      var page = args.object;
      page.onBackPressed = function () {
        console.log("Returning true will block back button default behaviour.");
        return true;
      };
      page.bindingContext = homeViewModel;
    }
    
    exports.pageLoaded = pageLoaded;
    

    What's tricky here is to find out which view handles back button press in your app. In my case, I used a TabView that contained pages but the TabView itself handled the event instead of current page.

提交回复
热议问题