Bring a specific dialog to front when having multiple dialogs - android

In my application, I have multiple dialogs of various types, and I invoke them like showDialog(dialogType).
I want a specific type of dialog to have a higher priority and to be shown always in the front. I even tried higherPriorityDialog.hide() to hide that dialog and higherPriorityDialog.show() to again show it, so that it should come to front, but no luck.
Is there any way I can do this?

Finally, I had to do something like this, keep a Vector of all the `Dialog' objects somewhere. Whenever a lowPriorityDialog comes, just finish highPriorityDialogs, and display that lowPriorityDialog and again display all the highPriorityDialogs. Hope I am not confusing you.

most likely you have to close all other dialogs before you open another. so call .close() on one and call .show() on another
I am not sure if android supports opening of multiple dialogs at a same time even if they are hidden.

Related

Need Advice to Use Fragment on Android Studio

here i want to develop an app that can run a simple quiz for psychology character test, but here i need some advice on how to show the question on the activity. Because i want to showing each question without moving to other activity.
This is my temporary layout (it's still ugly XD), but i want the question can appear on the inside of that red box. And when user press Next button, the question inside that box can changed but before it change the app has been stored the answer of previous question.
I really don't know how to use fragment or something like that.
NB. This is my mainActivity look :
For your app I would recommend not to use fragments at all, it will make your app much slower than it can be.
What I would do in your case, is only load the new question in the textbox on pressing next, and call an asynchronous function (asynctask) to perform the storage of the answer. This way the user sees no delays whatshowever.
If getting the next question required a server connection, use a progress dialog in between to show the user something happens.
Agree with Bas van Stein, using different fragments will slow down the app. What you could do is separate frequently changing part of your screen (e.g. question with answer options) into a fragment and reload it's data on Next/Answer button click. This will separate the code and make it easier to maintain and change it later.
On the other hand you could do it in the activity itself if you don't want to use fragment.

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.
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.

Get Android AlertDialog to appear on top of all activities at all times

Have been searching the web and stack overflow for this - I found a potential answer but it was not well explained enough for me as I am new to android.
I am working on an android app that displays alert dialogs at key times. I am currently having the app add the dialog to the current activity to make sure it appears.
However, sometimes the dialog can appear just as the user tapped a button to a new activity, causing the new dialog to be appearing on the previous activity and not seen unless the user hits back.
Another case is occasionally two dialogs will be appearing, and upon accepting the top-most the app brings the user to a new activity - leaving the second dialog on the previous activity and again unseen without hitting back.
The closest I could find was here: Keep dialog/activity always on the top
The above question was not fully explained in the answer. I tried following the advice by adding a new relative layout to each of my existing layouts with the ID "alert_layout" and I already had a BaseActivity all my activities extend from. When I need to show an alert I add it to the current activity's "alert_layout"'s context but it is yielding the same result. I am not sure how to add a layout that is never changed between activities - but even then, the AlertDialog is looking for a context to be added/shown to, not a layout, so I'm confused as to how to accomplish this.
I am new to Android development so I apologize if I'm missing the obvious, but I'm hoping there is an easy way to make sure AlertDialog is always on top of all activities and remains there across activity changes.
Thank you in advance for any assistance.
It's not going to work using Dialogs. You have to draw a View above the rest of your app's UI.
Android offers this possibility by drawing windows on top of all other applications. You need to have the SYSTEM_ALERT_WINDOW permission. Additional details in this question: What APIs in Android is Facebook using to create Chat Heads?

Create a new DialogFragment from another DialogFragment instantiated in another context

I am developing an Android library to ask the users of an app to give a rating on the Play Store.
The UX of the library consists in a few dialogs. Depending on the answer to the first question I might need to dismiss the current dialog, to show another dialog or to take the user to the Play Store.
Everything works nice unless I rotate the screen in the middle of the process.
I tried to solve my issues with the use of fragments. With fragments I am not losing anymore the state of the dialogs with the rotation but I am having troubles instantiating the second DialogFragment. The problem is that, after a rotation, the context of the first DialogFragment is no more active and it has no way to retrieve the new context. That results in exceptions every time I try to instantiate the new DialogFragment.
Is there any way I could solve this issue?
Thinking again at the problem, it seems that my design choice was wrong but I am not very experienced in Android development. Every advice will be more than welcome.
You should be using the listener pattern. Define an interface in each dialog and send clicks back to the activity. Let it create the dialogs.
see here
Also, if you need a context in the fragments, just call getActivity(), don't create an unnecessary context variable that could later give back an activity that's already gone. I hope that helps. I can't give a better answer without some code to see what's going wrong in this specific situation.

Showing a Dialog and still delivering clicks to the background?

I want to show a small Custom Dialog on top of the current user activity, but have clicks to the area outside of my Dialog delivered to the background (which would be the launcher, or another activity). I tried to create a transparent base-activity and have the Dialog shown on top of it, but clicks are registered on the transparent activity and not on whatever is behind it...
I know that a Popup has a setOutsideTouchable-Method, but setting this to true just dismisses the popup, rather than delivering clicks to the background, to my knowledge...
Thanks for your help,
Nick
Based on this clarification comment you posted on another answer...
"I want the Dialog to be shown system-wide, no matter which App the user is using at the moment..."
I don't believe what you want to do is possible and I'm happy about that. :) If you were allowed to popup a little dialog box over anyone else's app and still have the user be able to interact with the current activity... then you could easily trick the user into thinking that the little popup belonged to the current app and not yours which is acting from the background.
Imagine all of the evil you could do with something like that. Prompting for the user to reenter their email password when they are in the Email.app.. and then just storing it for malicious purposes, etc.
It isn't possible.. and SHOULDN'T be possible. If you need to notify the user of something, then you should use the built-in notification system. That's why it is there! :)
You can use a PopupWindow "Dialog like" and show it for the user in top of your activity. The outside events will be delivered to the main Activity.
I can't see the deeper meaning of exploiting the using the given usability patterns of android by doing what you ask for?
It is recommended and meaningful to stick to common patterns so users don't have to adapt in basic apps. That is, unless you are working on a game:)
"I want the Dialog to be shown
system-wide, no matter which App the
user is using at the moment..."
That is what the notification system is for. It allows you to tell something to the user without interrupting him in whatever he was doing.
System wide dialog popup are evil and gladly not implemented in Android.
Use the notification system : http://developer.android.com/reference/android/app/Notification.html
Also if you are ok with only a dialog above YOUR app, then the simplest way to let the UI be still responsive while your dialog is up, is simply to recreate a dialog in a relativelayout view and display this instead of the common modal dialog.
You could switch places so that the popup is actually "behind" the initial screen, but then set the initial screen as transparent.
Edit: This would only be applicable within an application of course.

Categories

Resources