Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NativeScript + angular 2 : Use of back button click listener

I am developing cross platform mobile application using NativeScript + Angular 2.

I want to handle back button click of the current View, i.e. when user clicks on back button in android device, i want to perform an action like killing / removing the current view from the stack.

For ex: In android platform (Native Development) we can use finish() method of the activity for removing it from the stack. We can handle onBackPressed() like -

@Override
public void onBackPressed()
{
    finish();  //Removes current Activity from stack
}

So is there any way to handle onBackPressed() and finish() method in NativeScript + angular 2? I googled a lot but didn't find any solution and also tried Frame.goBack() in NativeScript + Angular 2, but didn't worked for me. It works great in NativeScript + JavaScript.

UPDATE

I want to remove view permanently from the stack as it is not needed any more in application. It should display first time when app install.

For ex :

Like Login Screen

1) When app installs then Login screen should display and on next launch of the application, App will automatically skip the Login Screen and move to the Home Screen(This is working fine)

2) But the problem is when i press back button from Home Screen then app navigates to Login screen every time because Login screen still present in STACK. So that's why i want to remove login screen permanently from stack.

like image 654
Onkar Nene Avatar asked Nov 28 '25 08:11

Onkar Nene


1 Answers

What you need to implement going back is to inject Location and use the method back()

import {Location} from '@angular/common';

@Component({ ... })
export class MyComponent {
    constructor(private location: Location) { }

    public goBack() {
        this.location.back();
    }
}

At this point when the user goes back, you shouldn't worry about explicitly destroying the view as there is no mobile option for going "forward"

like image 77
Nick Iliev Avatar answered Nov 29 '25 22:11

Nick Iliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!