Kalisky
Kalisky

Reputation: 1161

DialogFragment trying to open after coming back from the background

I'm showing a DialogFragment and when the user goes back to the background and then goes back to the app, the dialog is displayed.

Is there a way not to display it when coming back from the background?

I tried this (EDIT: moved this from onStop to onPause as some suggested):

  @Override
  public void onPause() {
    if (dialogFragment.isVisible()) {
       dialogFragment.dismissAllowingStateLoss();
    }
    super.onPause();
  }

But got this:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference

Any ideas?

Upvotes: 3

Views: 1608

Answers (3)

GVillani82
GVillani82

Reputation: 17429

You can try using dismissAllowingStateLoss()

dialogFragment.dismissAllowingStateLoss();

instead of:

dialogFragment.dismiss();

However this should be required since you are dismissing it in the onStop(). You should try to dismiss it inside the onPause().

Upvotes: 2

Mike Cwiklinski
Mike Cwiklinski

Reputation: 151

I think better place to hide dialog as in above answer would be onPause().

Upvotes: 0

Patel Hiren
Patel Hiren

Reputation: 305

Add this method.

 @Override
    protected void onDestroy() {
            super.onDestroy();
            if(dialogFragment != null && dialogFragment.isVisible()) {
                dialogFragment.dismiss();
            }

        }

Upvotes: 0

Related Questions