Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise one DialogFragment above another?

Say I have several dialog fragments that are shown in response to messages and events that can arrive in any order. Normally, the last dialog shown will be on top. Is there a way to show a dialog fragment under an existing one, or change their z-order after they are shown?

It should be pretty rare for my app to show more than one dialog at a time, but it could happen. There is one particular dialog that should always be on top whenever it's visible.

like image 235
Kevin Krumwiede Avatar asked Jul 01 '15 09:07

Kevin Krumwiede


People also ask

Is DialogFragment deprecated?

This method is deprecated. androidx.

How many styles of AlertDialog are there?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I destroy DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.


1 Answers

A dialog creates an application sub-window. Android's window manager (WindowManagerService) automatically computes window's z-order depending on its type and stores it in WindowState's mLayer field. Internal Android classes have access to this field and change window's z-order sometimes, but this API is not exposed to Android SDK. So it seems that the only way to affect dialog's z-order is to recreate it.

Everything I wrote above is just a result of a brief investigation of Android's source code so I may be wrong. And maybe there's some hacky way to do what you want using reflection and accessing private fields and methods. But I'm not sure it's a good idea to try and do it. In my opinion it would be better to have just a single dialog or even activity, and manage fragments within it.

like image 64
Michael Avatar answered Oct 17 '22 01:10

Michael