How to raise one DialogFragment above another? - android

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.

Related

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.

In-app only notifications / interactive toasts

Our Android app currently has a large number of dialog and alert boxes. I'd like to switch these to toasts, but there's a problem - some of them require the user to choose whether to view more info or just dismiss the popup. It doesn't look like there's a way to do this with toasts.
Is there any existing Android library that supports tappable toasts (i.e., you tap it and it triggers a function call to a listener in the app, sort of like a notification)? If not, is there a recommended alternative for this "tap-here-to-do-something-otherwise-I'll-just-vanish-in-a-few-seconds" UI pattern, or should I just roll my own fragment class for it?
I have to do something similar to my app so I wrote, DropViewNotification, a boiler plate to make it happen by animating the so-called notification into the screen. It doesn't do automatic dismiss as this should only act as a tool.
It accept any kind of view to make it versatile as I need to put at least two or three obvious view into it (TextView, ProgressBar, ImageView). You can switch it's content on the fly if you want to. Animation can also be customized for both showing and dismissing of the notification and the main content.
In real-life you might want to consider adding controller class to handle the display of the content and auto dismissal, etc. Hope it's of some use to you.

Bring a specific dialog to front when having multiple dialogs

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.

Activities, Views and Dialogs in Android. Proper application structure for game

I was in the midsts of tinkering in android with the goal of trying to make this small exploration game. I actually got pretty far so far: a nice sprite system, a title screen with a main menu that is all in game code (no UI buttons or anything) that launch various activities. An activity that loads this cool map view, that when clicked on will load up another view of a more detailed "zoomed in" action part of a map. It all works pretty well but now I am left wondering how well I am going about this.
(What happens to a view when you instantiate and then move the set the context to a new view? I am guessing as long as the activity is alive all the instantiations of various views that an activity uses are still good? And in an activities onPause is when id have to save the data thats changed in these views to persist the state in the event the user quits the game etc.. ?)
I have three menu options at the start of the game. The main one is:
New Game
The new game, I launch my main map
activity that can launch three views:
First two are a loading screen for
the second, a map screen.
Third view is a more detailed action
orientated part that uses this sprite
engine I developed.
It all works pretty good as in the map view you click on a cell it tells the Calling Activity through a listener to start the detailed action view, and passes in the ID of the cell (so it knows what objects to load in the detailed view based on the cell clicked in second view). (This took me awhile to figure out but someone helped here on another question to get this.) Reference that helped me on this part here. I also used the various information here on ViewSwitcher.
I got even a yes no dialog box in the second view that asks if they really wanna goto that cell when they click on it, a yes tells the calling activity to set the new view. What helped me to get this working was this reference here. I also had to fiddle with the calls to getContext and that, I am still not 100% sure what getResources and getContext do for me.
So is it bad practice to call and load a dialog from a view? If it is, then I don't understand how if I have an event in the view that needs to pop up a dialog how I do that? Call back to the activity and let the activity handle it the dialog response and then call a method on the view for the response?
So far all this works great, I did have a bit to learn about threads too. I spawn a different thread for my MenuView (handles the detection of button clicks and responses), my CellMap view which handles the cool big map that users can click on to see the indepth view which is my SystemView view.
So basically the activity call list is like this:
Home.Activity -> ScrollMap.activity which listens to CellMap wher ethe Yes/No dialog apperas (in the view of CellMap) and if the answer is yes to the question, then the onTouchEvent starts up the SystemView view using
private CellMap.MapClickedListener onTouchEvent=
new CellMap.MapClickedListener() {
#Override
public void onMapClick(int id) {
setContentView(new SolarSystem(theContext,id));
}
};
To help anyone else, that listener had to be defined in my CellMap class. Then in the ScrollMap activity when I am about to start the CellMap I just call a method to CellMap sets the map click listener. So far this is the only way I have been able to get data from a dialog (in this case a it was clicked so set the new view) back to the calling activity. Is this proper practice?
Now my biggest question is, I have a playerObject class I want to initialize on new game event (I know how to detect that push) and that then is available to any view in any activity for the life time of the cycle. So far the game activity has just two (3 if you count the loading progress bar) views. I want to get the players name, is that another activity or view? I cannot find a decent tutorial of just how to make a simple input dialogg box that would return a string.
How do i go about passing this stuff around? After I get the players name from wherever that edit box is to be loaded, I want to set the players name in the class and pass that class instance off to another view or activity so they may get at that data. Everything I have searched on this seemed like overkill or not the right way. Whats the best way to pass this "playerClass" around so that I can access it from other views (to say display the players name in every view).
I also have my Home Activity that waits for an onClick on 3 buttons, it then launches their activity that shows the view. I realize a continue game and new game are the same activity and will just change in data loaded off the drive to restore a save game. When the new game button is clicked I would love to get the players name they want to use and of course set the member of the playerClass to this name so it persists.
Where do I call (an edit dialog is it?) the edit dialog from? (more over how do I build one that can take an okay button and input from keyboard etc). Id also like to make it pretty, maybe I could have a simple view that has one edit box in it and a nice image background (though I haven't figured out how to place the edit boxes to fit nicely matching a background i draw for it. I guess id like an edit dialog I can skin to look how i want it to and fit the look of the game).
I am actually kinda happy what I got so far its looking not to bad, just not sure about some specifics like getting user input for a name of the player. Storing that information and passing it then to other activities and views.
Is it better to have one GameMain activity, and a ton of views:
Map view
Character sheet view
inventory view etc etc?
Or Should there be diff activities in there somewhere as well?
You've written a lot here, so I'm go gonna go ahead and answer the questions I think you're asking.
First, how can one share information between activities and views? If you're just sharing it between views in a single activity, store it in the instance of the Activity subclass.
Sharing between activities requires a little bit more. Instead of using an instance of the default Application class, you can create a subclass of Application and use that instead. Put your data in fields of that subclass. To tell your app to use your subclass, modify the app manifest like so:
<application
....
android:name=".YourAppClassNameHere">
....
</application>
As for getting user input, the simple answer is use the built-in EditText view. since you seem to want to give your game a more "custom" style, though, you may need to go about creating your own editable textbox. That and buttons should allow for most sorts of basic user input.
Now, good practice vis-a-vis activities and views. I'm not aware of a standard for this, but when designing I generally try to think of activities as more conceptually separate elements, whereas views are conceptually interwoven. the line between the two is dependent, in part, on the scope of the app; an app with a narrow focus can afford to break more actions down into different activities than can one with a much broader focus.
Your "GameMain" example is a bit on the fence, but I think you've made the right choice: the activity is "playing the game," as opposed to a menu or high-scores table, and the views present different aspects of the game being played.

Categories

Resources