# ionViewCanLeave method strange behavior

**URL:** <https://forum.ionicframework.com/t/ionviewcanleave-method-strange-behavior/97020>\
**Category:** ionic-v3\
**Created:** [July 5, 2017, 11:53am UTC](https://forum.ionicframework.com/t/ionviewcanleave-method-strange-behavior/97020 "2017-07-05T11:53:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [July 5, 2017, 11:53am UTC](https://forum.ionicframework.com/t/ionviewcanleave-method-strange-behavior/97020/1 "2017-07-05T11:53:24Z")

</div>

Dear All,

I want to prompt the user that changes will be discarded if he leaves the page. Please see code below.

```auto
  ionViewCanLeave(): Promise<boolean> {
    // apply only when going backward
    if (this.moveBack) {
      return new Promise((resolve: any, reject: any) => {
        let alert = this.alertCtrl.create({
          title: 'Warning',
          message: 'Are you sure you want to leave this page? All changes made will be discarded.'
        });
        alert.addButton({
          text: 'Stay',
          handler: () => {
            reject();
          }
        });
        alert.addButton({
          text: 'Leave',
          role: 'cancel',
          handler: () => {
            //clear temp area for saving of return
            this.SaveReturnService.clear();
            resolve();
          }
        });
        alert.present();
      });
    }
  }

```

The prompt is displayed. If the users chooses stay, it is ok but if it chooses ‘leave’, there is strange behavior. For instance see below pages.

A -\> B -\> C

If i am on page B and choose to leave the page, it will pop to A and then to push C. Just wanted to know if there is anyone who has encountered this issue.

Thanks,  
Ashley

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [July 5, 2017, 8:26pm UTC](https://forum.ionicframework.com/t/ionviewcanleave-method-strange-behavior/97020/2 "2017-07-05T20:26:54Z")

</div>

> [@ashley\_shookhye](#):
>
> // apply only when going backward  
> if (this.moveBack) {

What does this mean?

> [@ashley\_shookhye](#):
>
> //clear temp area for saving of return  
> this.SaveReturnService.clear();

What is this?

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [July 6, 2017, 4:57am UTC](https://forum.ionicframework.com/t/ionviewcanleave-method-strange-behavior/97020/3 "2017-07-06T04:57:58Z")

</div>

Hello,

this.moveback is used for the fact that ionViewCanLeave should be called only when user navigates backwards. If the user chooses to leave, the saveReturnService will clear the data held in memory.

Regards,  
Ashley
