Proper way of dismissing DialogFragment while application is in background Proper way of dismissing DialogFragment while application is in background android android

Proper way of dismissing DialogFragment while application is in background


DialogFragment has a method called dismissAllowingStateLoss()


This is what I did (df == dialogFragment):

Make sure that you call the dialog this way:

df.show(getFragmentManager(), "DialogFragment_FLAG");

When you want to dismis the dialog make this check:

if (df.isResumed()){  df.dismiss();}return;

Make sure that you have the following in the onResume() method of your fragment (not df)

@Overridepublic void onResume(){  Fragment f = getFragmentManager().findFragmentByTag("DialogFragment_FLAG");  if (f != null) {    DialogFragment df = (DialogFragment) f;    df.dismiss();  }  super.onResume();}   

This way, the dialog will be dismissed if it's visible.. if not visible the dialog is going to be dismisded next the fragment becomes visible (onResume)...


This is what I had to do to achieve what you want:I have a Fragment activity on which i was showing a dialog fragment named fragment_RedemptionPayment which is globally declared at the top. The following code dismisses the DialogFragment if it was showing before the activity goes in background and comes back in foreground.

     @Override        public void onResume() {            super.onResume();                    if(fragment_RedemptionPayment.isVisible()){                fragment_RedemptionPayment.dismiss();            }}