Android: Under What Circumstances Would a Dialog Appearing Cause Onpause() to Be Called

Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.

A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example, it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case, displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.

Activity's onpause method not getting called after showing AlertDialog

I have tried below code to check Activity's call backs, As of my
understanding whenever dialog comes on top of Activity, Activity's
OnPause() method should call. When dialog disappears Activity's
OnResume() should trigger.

NO. Documentation is bit confusing. When you call a dialog on top of activity, onPause() of activity will never call. Think it in other way, dialog always tied-up with calling activity, and if that activity in not in running state, how that dialog would be visible?

Actually, onPause() will only call when you are calling another activity as dialog (using dialog theme, or an activity which does not cover full screen).


From documentation

onPause()

  • A new, semi-transparent activity (such as a dialog) opens. As long as the activity is still partially visible but not in focus, it remains paused.

You can also read Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

Android life cycle event on dialog

This answer isn't fully correct

Check other answers below for better info. it was accepted a long time ago and I can't delete it.


First you should understand the Android Lifecycle.

As you can see, onPause in called when the activity is Paused, which is when your dialog appears, and onResume after you come back to the activity, when it get focus again.

As another user suggested, you can use some Log tags to see what is happening on the console or Toasts to see it on the screen: How do I write outputs to the Log in Android?

Android: Scenario where onPause is called but not onStop?

I figured this out a while later, but forgot to post my answer here.

The instance where I noticed that onPause() was called, without an immediate subsequent call to onStop() was when I got a notification from a different application when a different app was open on my phone.

For example, say you have Facebook running on your phone currently. If you receive a notification from WhatsApp (in the pop-up dialog box), your Facebook activity will be paused. If you close the WhatsApp pop-up during this time, the Facebook Activity will get Resumed. On the other hand, if you open WhatsApp from the pop-up message, your Facebook Activity will be stopped.

Activity crashes when dialogFragment is opened and onPause() is called

You really should not be making anything Serializable that should die when the lifecycle of a fragment/activity die. For this solution, remove the interface on the getInstance(). You should not pass interfaces via fragment creation. You should create a setter for the interface. I don't have nearly enough information to solve the issue but, I believe this may be the solution. Let me know if it works, so I can delete if it it doesn't.

Dialog

public class RequestDialog extends DialogFragment {
private MyDialogInterface callbackListener;

public interface MyDialogInterface {
void onClickContinuarEvent(int permisoRequerido);

void onClickCancelarEvent(int permisoRequerido);
}

public void setCallbackListener(MyDialogInterface callbackListener) {
this.callbackListener = callbackListener;
}
public static RequestDialog getInstance( boolean activationMode) {
RequestDialog fragmentDialog = new RequestDialog();
Bundle args = new Bundle();
args.putBoolean(REQUIERE_ACTIVACION_MANUAL, activationMode);
fragmentDialog.setArguments(args);
return fragmentDialog;
}
}

Create Dialog

RequestDialog requestDialog = RequestDialog.getInstance(true);
requestDialog.setCallbackListener(new RequestDialog.MyDialogInterface() {
@Override
public void onClickContinuarEvent(int permisoRequerido) {

}

@Override
public void onClickCancelarEvent(int permisoRequerido) {

}
});
requestDialog.show(getSupportFragmentManager(), "REQUEST_DIALOG");

When is an activity paused without soon after being stopped?

OnPause is called when an Activity is still in FG visible and running but is obscured such as when you start a Dialog the activity is still running in the FG and visible but its obscured by the Dialog, this is OnPause.

onStop is when we are completely in BG such as turned to new activity or so, it is still active and running but untouchable and not visible.

in a nutshell



Related Topics



Leave a reply



Submit