Android's Dialog provides helper isShowing() to determine if the dialog is shown or not, but it doesn't necessarily mean that the Dialog window is shown. The dialog built using the activity's context is requested to be shown via show(). This API involves a communication between multiple processes starting from the application process's Dialog ViewRootImpl and Framework's Window Manager Service and Surface Flinger and there is a tiny delay between when the original request and the actual window being rendered. Eventually, a new Window is created and tracked by framework. This new window would now host the requested dialog. This entire process involves a set of IPC between the application and framework.
However, any attempt to check the status via isShowing() after show() is going to return true.
AlertDialog dialog = builder.create();
dialog.show();
Log.d(TAG, "Dialog Status [" + dialog.isShowing() + "]");
This doesn't really make any sense especially as the UI thread requesting for the dialog is still executing and isn't finished. So is there any other reliable way to ensure or check if the Dialog is actually shown?
As the dialog is shown in a new window, the window corresponding to the activity requesting for the dialog should actually lose focus and the new dialog window should gain focus.
getWindow().getDecorView().getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d(TAG, "Activity window focus [" + hasFocus + "]");
}
});
Requesting for the window focus change notifications serves as a good point to determine the actual showing status of the Dialog. Why does this matter? Ideally, for an end user the delay between requesting a dialog and the actual dialog being shown might not be noticeable. However, test frameworks could actually exploit this tiny window and could actually trigger click events in the Activity.
However, any attempt to check the status via isShowing() after show() is going to return true.
AlertDialog dialog = builder.create();
dialog.show();
Log.d(TAG, "Dialog Status [" + dialog.isShowing() + "]");
This doesn't really make any sense especially as the UI thread requesting for the dialog is still executing and isn't finished. So is there any other reliable way to ensure or check if the Dialog is actually shown?
As the dialog is shown in a new window, the window corresponding to the activity requesting for the dialog should actually lose focus and the new dialog window should gain focus.
getWindow().getDecorView().getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d(TAG, "Activity window focus [" + hasFocus + "]");
}
});
Requesting for the window focus change notifications serves as a good point to determine the actual showing status of the Dialog. Why does this matter? Ideally, for an end user the delay between requesting a dialog and the actual dialog being shown might not be noticeable. However, test frameworks could actually exploit this tiny window and could actually trigger click events in the Activity.
No comments:
Post a Comment