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?
source to share
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()
.
source to share