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

故事扮演 提交于 2019-11-26 15:04:24
hackbod

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.

I've been doing quite a lot of code with dialogs, including the AlertDialog that you mention, and I've also tried to check if onPause() is being called on the activity when the dialog pops up, but thus far my conclusion is that the activity simply keeps running and that onPause() is not called.

Not sure if it helps, but at least you now know that there are others who experience what you're experiencing :-)

Its wrong that activity remains no longer at top of activity stack in onPause phase.

Condition an activity to be onPause state -

  • Activity partially visible e.g. dialog on activity.
  • The Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager.

    e.g Home button pressed causes activity to go in onPause(). Still at top of stack.

In fig 1. Activity3 will be destroyed and removed from top stack

In fig 2. Now Task A goes to background but Activty X still on top of stack . If you override onPause() method int this state

Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes.

Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.

I think I remember reading in an earlier version of the Android Lifecycle that onPause was called when none of the activity is on display. i.e. if a bit of your activity is still visible under a popup, onPause will not be called.

Maybe some other experts can vouch for this behavior?

Karl

In my slightly weird experience onResume gets called with dialog.setCanceledOnTouchOutside(true); but onPause never gets called.

That being said, I think the documentation might focus on system dialogs (e.g. low on battery).

@hackbot

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.

everything depends on implementation...

what is a Dialog ? is a Window added to Display by WindowManager/// so the window when it shows is on top of everything .... (Z order)

what is activity... is "thing" that also creates its window....

when a dialog is shown or it's window comes visible on top of an existing activity, then it overrides partial the activity window so existing activity will move to partially invisible state and you will get call to onPause() from ActivityThread.

but to be sure we also need to consider here a one think...

the state of window if is a standalone window shown on top or it is a child window and a parent of it is a activity window....

so when we know

  • the Window.LayoutParams (FLAGS) we use to add
  • and what IBinder is used for the Window to show

we will khow how the activity will behave when windows are shown each over other .. as each winndow has a callbacks they are used by activity or dialog to manage their states...

involved components:

  • android.os.IBinder

  • android.view.Window

  • android.view.Window.Callback

  • android.view.WindowManager

  • android.view.WindowManager.LayoutParams

  • android.view.Display

btw:

if you want to know the windows on screen [ applicable only for the process you own - as window belongs to process and those are Sandboxed - each processs is a separate JVM strictly saying "ART" ] you can use a replection see :

  • android.view.WindowManagerImpl
  • android.view.WindowManagerGlobal

onPause() is called every Time when an Activity goes background and Dialog or other Activity comes foreGround. This is done to give first priority to something with which the user is interacting. e.g: assume you are in homescreen (which in turn is an activity) of an application, the homescreen is said to be in foreground. and when you go to next screen by pressing some button or a dialog appears the next screen/Activity/Dialog comes to foreGround and homecreen goes to backGround, which just means homeScreen's onPause() method got called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!