DialogFragment trying to open after returning from background

I am showing a DialogFragment and when the user goes back to the background and then returns to the application, the dialog is displayed.

Is there a way to not display it when returning from background?

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

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

      

But it turned out:

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

Any ideas?

+3


source to share


3 answers


You can try using dismissAllowingStateLoss ()

dialogFragment.dismissAllowingStateLoss();

      

instead:



dialogFragment.dismiss();

      

However, this should be necessary since you are rejecting it in onStop()

. You must try to reject it inside onPause()

.

+2


source


Add this method.



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

        }

      

0


source


I think it's better to hide the dialog like the above answer would be onPause ().

0


source







All Articles