Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a view from the back history - Ionic2

Tags:

ionic2

Anyone knows how to remove a view from the back history (or navigation stack) in ionic2?

In Ionic 1 I solved this with

this.$ionicHistory.nextViewOptions({
  disableAnimate: true, 
  disableBack: true
});

Would be really useful, for example, to fully remove the login page of my application from the history once a successfully login was performed.

Just not showing the back button isn't enough in such case, since Android terminals got their own physical back button on the devices.

I tried, after my login function returned a successful promise and before pushing the next page in the stack:

this.navController.pop();

or

this.navController.remove(this.viewCtrl.index);

but unfortunately both weren't successful :(

like image 389
David Dal Busco Avatar asked Jul 14 '16 08:07

David Dal Busco


2 Answers

obrejacatalin on the https://forum.ionicframework.com/t/solved-disable-back-in-ionic2/57457 found the solution

this.nav.push(TabsPage).then(() => {
  const index = this.nav.getActive().index;
  this.nav.remove(0, index);
});

so I guess it's important to push the next page first, wait for the promise answer and then remove the current view

like image 152
David Dal Busco Avatar answered Sep 21 '22 20:09

David Dal Busco


To remove one backview you need to use startIndex and count of pages to remove from stack.

    this.navCtrl.push(NextPage)
    .then(() => {
      const startIndex = this.navCtrl.getActive().index - 1;
      this.navCtrl.remove(startIndex, 1);
    });

See this document for more options like removeView(viewController): https://ionicframework.com/docs/v2/api/navigation/NavController/#remove

like image 27
Ilkka Nisula Avatar answered Sep 22 '22 20:09

Ilkka Nisula