11

Can you please explain which activity life cycle method is called when a dialog comes on the application? I'm confused whether its is calling onResume() or onPause() method.

Thanks

Priya R G
  • 543
  • 4
  • 10
  • 21

8 Answers8

23

OnPause() is not called in all types of dialogs.

For Example, when an AlertDialog or DialogFragment is used, it will never call OnPause(), since they are a part of the activity.

However, if a dialog appears from System for a permission or some other app shows a Dialog over the activity it will only call OnPause() since a new activity isn't started and only the foreground focus is shifted from the activity to the Dialog Box.

For Example, when we enable Whatsapp to send a message popup, if the popup comes while your activity is running, it will call OnPause() only.

You should try this on your own for better understanding.

Anubhav Mittal
  • 427
  • 3
  • 7
15

Watch out, few of proposed answers are wrong. This old one have most of truth, but not whole truth. And that new one seems to complement my answer (haven't checked by myself).

This is not true that onPause is called after dialog appears. This dialog would have to be written on separate Activity to cause onPause call. But dialogs are usually written on DialogFragment from support library - reference

you should use a DialogFragment as a container for your dialog

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

PrzemekTom
  • 1,328
  • 1
  • 13
  • 34
  • Just want to know, is any activity's lifecycle event called when dialog.dismiss() called(Here dialog extends Dialog) – B.shruti Feb 27 '19 at 05:45
  • 2
    @B.shruti `android.app.Dialog`? Nope, `dismiss` doesn't execute any Activity callbacks. Why it should?https://developer.android.com/guide/components/activities/activity-lifecycle - you could understand that for `dismiss` Activity goes to background. NO. As I said in the answer: `dialog would have to be written on separate Activity to cause onPause call`. Like here: https://stackoverflow.com/questions/1979369/android-activity-as-a-dialog Do not use `Dialog`, especially in MVVM era: https://stackoverflow.com/questions/7977392/android-dialogfragment-vs-dialog – PrzemekTom Feb 27 '19 at 13:09
  • what about an alertdialog instead of a dialog fragment? – Cyph3rCod3r May 07 '19 at 12:55
  • 1
    @Dr.aNdRO DialogFragment synchronizes its lifecycle with FragmentManager. https://developer.android.com/reference/android/app/DialogFragment#lifecycle so it's added to back stack and back button press is handled. It's broader topic, cause actually you could use AlertDialog. As usually, it depends. More: https://developer.android.com/reference/android/app/DialogFragment – PrzemekTom Aug 12 '19 at 15:15
12

onPause is not called because you are still in current activity, so when you are showing dialog on current activity no activity life cycle method will called.

Amit Desale
  • 1,281
  • 3
  • 16
  • 43
2

Activity inside if open any dialog, then that dialog not affect to activity life cycle. so i already try this one. so onPause() not called. if any doubt please implement your self you can get more clarity.

Nagendran P
  • 96
  • 1
  • 5
2

I have checked the lifecycle of Activity is changed or not using a AlertDialog and I can see no changes in the lifecycle of activity. When a dialog appears no onPause is called and when a dialog is cancelled no onResume is called.

No lifecycle changes happens if the dialog is called

 D/lc1: onCreate
 D/lc1: onStart
 D/lc1: onResume
 I/System.out: lc1 AlertDialog is created
 I/System.out: lc1 clicked yes
 I/System.out: lc1 AlertDialog is created
 I/System.out: lc1 clicked no
 I/System.out: lc1 AlertDialog is created
 I/System.out: lc1 clicked cancel
 D/lc1: onPause
 D/lc1: onStop
Rohit S
  • 714
  • 5
  • 7
1

it calls onPause()

When a dialog comes on top of an existing activity, then existing activity will move to partially invisible state by calling onPause().

0

OnPause() is not called in all types of dialogs, check other answers for more details since I won't copy everything here.

E_net4
  • 27,810
  • 13
  • 101
  • 139
George
  • 6,886
  • 3
  • 44
  • 56
  • 3
    this is not right. when dialog appears on the current activity no life cycle method is called. You can verify it by adding logs to your life cycle activity. – cantona_7 Sep 03 '20 at 07:54
  • This should not be an accepted answer. – N J Nov 08 '22 at 15:12
0

This is given on the official Android website

https://developer.android.com/guide/components/activities/activity-lifecycle

The opening of a new, semi-transparent activity, such as a dialog, pauses the activity it covers. As long as the activity is partially visible but not in focus, it remains paused.