Ionic 2 close popover on back button click when popover is opened or open an alert control when popover is closed

I am writing an ionic 2 app. I want to close the popover on back button click when the popover is open or open an alert control on backbutton click when popover is not opened. below is my code. But when the back button is clicked…the popover does not close.

backButtonAction() {
if(this.popover.present())
{
this.popover.dismiss();

}
else if (this.popover.onDidDismiss()) {
let alert = this.alertCtrl.create({
title: 'Exit App',
message: 'Do you want to close this app?',
buttons: [

{
text: 'Yes',
handler: () => {
console.log('yes clicked');
this.platform.exitApp();
this.viewCtrl.dismiss();
this.popover.dismiss();
}
},
{
text: 'No',
role: 'cancel',
handler: () => {
console.log('no clicked');
this.viewCtrl.dismiss();
this.popover.dismiss();
}
}

]
});
alert.present();

}
*/
}

Please indent and format your code (</> button in the toolbar above the input). It is rude and impolite posting such a “dump” of code and expecting us to parse and understand it. Thank you.