# Dismiss alert when another alert is initiated

**URL:** <https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831>\
**Category:** ionic-v3\
**Created:** [December 10, 2016, 10:11am UTC](https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831 "2016-12-10T10:11:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [December 10, 2016, 10:11am UTC](https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831/1 "2016-12-10T10:11:22Z")

</div>

One alert is shown on the screen…in the background one process is going on which again initiates an alert…so now the previus alert should be dsimissed and the next one must be shown.

can anyone tell hove to achieve this?

---

<div class="post-metadata">

**Author:** ![lannie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lannie/32/65321_2.png) [@lannie](https://forum.ionicframework.com/u/lannie)\
**Post date:** [December 12, 2016, 12:43am UTC](https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831/2 "2016-12-12T00:43:46Z")

</div>

alert has a method alert.dismiss() to close it, but I’m guessing you already know that. I think what you might want to do is create an alert service so everybody uses the same alert controller. Here’s mine:

```
export class Services {

  alert: any;

  constructor(private alertCtrl: AlertController) {}

  public Alert = (title:string, message?:string, actionLabel?:string, actionCallback?:any, cancelLabel?:string):void => {
    let buttons:Array<any> = [
        {
          text: cancelLabel || 'Cancel',
          role: 'cancel',
          handler: () => {
          }
        }
      ];
    if (actionLabel) {
      buttons.push(
        {
          text: actionLabel || 'OK',
          handler: actionCallback
        }
      );
    }
    this.alert = this.alertCtrl.create({
      title: title,
      message: message,
      buttons: buttons
    });
    this.DismissLoading
    this.alert.present();
  }
}

```

Inject the service into all your components, and then just use this.services.alert(‘Hello!’) whenever you need to alert. I think that this will automatically get you what you are looking for, a new alert will replace the old one. If that is not happening, you can check whether an existing alert is open and close it like this:

`if (this.services.alert && !this.services.alert._detached) { this.services.alert.dismiss(); }`

(I don’t like using the internal variable \_detached but I don’t see another way to tell if an alert exists but has been dismissed.)

---

<div class="post-metadata">

**Author:** ![Derekbaker](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Derekbaker](https://forum.ionicframework.com/u/Derekbaker)\
**Post date:** [November 6, 2018, 2:57pm UTC](https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831/3 "2018-11-06T14:57:17Z")

</div>

I’ve been looking for something similar. Plus a way to dismiss the alert from any other component. This is my actual solution:

```auto
import { Injectable } from '@angular/core';
import { AlertController, Alert } from 'ionic-angular';

@Injectable()
export class AlertService {
  private alert: Alert;

  constructor(private alertCtrl: AlertController) {

  }

  public present(options) {
    this.dismiss();
    this.alert = this.alertCtrl.create(options);
    this.alert.present();
  }

  public dismiss() {
    if (this.alert && !this.alert._detached) this.alert.dismiss();
  }

}

```

I’m still working on this because I have a case in which dismissing an alert will immediately present another one. So, because the new alert is using the same service, the service will close the previous alert, but ionic AlertController will try to close it too after it’s already closed, returning an “Uncaught (in promise): removeView was not found” error

---

<div class="post-metadata">

**Author:** ![sreeragsp](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sreeragsp](https://forum.ionicframework.com/u/sreeragsp)\
**Post date:** [March 13, 2019, 11:21am UTC](https://forum.ionicframework.com/t/dismiss-alert-when-another-alert-is-initiated/72831/4 "2019-03-13T11:21:39Z")

</div>

[https://forum.ionicframework.com/t/find-out-if-alert-over-active-view/88232/2?u=sreeragsp](https://forum.ionicframework.com/t/find-out-if-alert-over-active-view/88232/2?u=sreeragsp)

checkout this link.it will surely help you out.
