Search code examples
angularnativescriptnativescript-angular

Nativescript-Angular handling back button to change vizibility


I am trying to change visibility of an element in the back button event.

 <ActionItem icon="~/images/menu_3_dots.png"  ios.position="right" android.position="right"   *ngIf="isActionItemVisible"></ActionItem>

 constructor(private router: Router, private page: Page) {        
         application.android.on(application.AndroidApplication.activityBackPressedEvent, (args: any) => {           
                        args.cancel = true;
                        console.log("Is backbutton pressed !");
                        this.isActionItemVisible= false;
        });          
}         

The variable become false but the element doesn't hide.


Solution

  • You sometime need to tell angular to do a detection cycle to apply changes :

    constructor(private router: Router, private page: Page, private cdr: Change
    DetectionRef) {        
             application.android.on(application.AndroidApplication.activityBackPressedEvent, (args: any) => {           
                            args.cancel = true;
                            console.log("Is backbutton pressed !");
                            this.isActionItemVisible= false;
                            this.cdr.detectChanges();
            });          
    }