Barnali Bhattacharjee Barnali Bhattacharjee - 17 days ago 13
Android Question

Back Press From the Fragment

I have one query and need help regrading it.

I have two Fragment in my app, let say Fragment A and Fragment B , when my app opens Fragment A will appear first (and it contain one button while pressing button it call fragment B ).Now Fragment B contains one Button (let's say Button C),while pressing Button C the popwindow will appear.

Problem i am facing ,when i back press from the Fragment B, the popwindow dismissing ,that working fine but it loading fragment A.

I don't want to load Fragment A on Back Press. On Back Press ,if pop window is showing then it should dismiss and remain in fragment B and if popwindow is not showing then it should go out of the app.

I tried : I created the interface in fragment B and getting the instance of the popwindow in Activity.

@Override
public void onBackPressed() {
super.onBackPressed();

if(popupWindow!=null && popupWindow.isShowing()){
popupWindow.dismiss();
backpopwindow.dismiss();
}
}

@Override
public void onHandleRequest(PopupWindow popupWindow, PopupWindow backpopwindow, String fragmentName) {
if (fragmentName != null) {
this.fragmentName = fragmentName;
if (popupWindow != null) {
this.popupWindow = popupWindow;
this.backpopwindow = backpopwindow;

}
}
}

Answer
@Override
public void onBackPressed() {  

  if(popupWindow!=null && popupWindow.isShowing()){
     popupWindow.dismiss();
     backpopwindow.dismiss();
  } else {
    finish();
  }
}

This will close the popupWindow if it is visible, or else finish the activity

Comments