Amine Messabhia
Amine Messabhia

Reputation: 540

Android back button close popup if opened

I have a main activity , and a popup.xml file that is included in the activity

the problem is when i press the back button , it closes the app directly , whether the popup is opened or not

i got the idea to override the onClick method, add a boolean that will be true when the popup is opened , and false otherwise , then add this condition in the onClick method

i'm still a noob with Android Studio , would anyone please guide me through ?

Thank you.

Upvotes: 0

Views: 1160

Answers (3)

Punithapriya
Punithapriya

Reputation: 131

 @Override
public void onBackPressed() {
    if(!(Activity).isFinishing){
//activity is not yet finished
}else{
//activity finishes
super.onBackPressed();
}
}

Upvotes: 0

Appyx
Appyx

Reputation: 1185

Just override the following method in your Activity:

@Override
public void onBackPressed()
{
     //Do whatever you want before the back button should trigger
     super.onBackPressed();  // call this only if you want to close the app
}

Upvotes: 0

Manohar
Manohar

Reputation: 23384

override the onBackPressed in your activity and check if popup is showing. if popup is showing then close popup else do general back press action

 @Override
public void onBackPressed() {
    if(popupWindow.isShowing())
        popupWindow.dismiss();
    else
    super.onBackPressed();
}

Upvotes: 3

Related Questions