I'm new to Android and I'm programing an application with multiple user interfaces(windows).
I find it a little hard to define new activity for each window so I end up with using Dialogs more than activity. But there is a scenario where I need to sequentially show multiple dialogs.
In c# showing Dialog is blocking operation. But I don't know how to do that in Android.
if there, I welcome any alternatives.
If you want to show a sequence of dialog, you can use the onclick listeners. From one dialog, open the following one. ( dialog interface for the listeners)
and if you want to block your program, so the user has to click on the dialog, set the dialogs not cancelable (setCancelable)
OK with no code reference I would say the easiest way would be using ondimiss listeners in each dialog for the next one to be called. You can check out this short example to get an idea of implementation (note they are using ondismiss for something else).
http://android-er.blogspot.com/2011/11/cancel-progressdialog.html
Related
I have coded two dialogs in Android, which individually works.
But the first dialog is rapidly replaced by the second dialog without permetting an answer.
My question is : how to force an order of execution (my code can wait for an answer on the first dialog).
My solution could be to exchange dialogs execution order to be sure that the second dialog is the first launched.
But I wonder if the is a better solution ?
I have seen a lot of Android examples on the site, but not really answering my question.
There are two solutions.
First use dialogFragment with Navigation, and upon input from dialog 1, navigate to dialog 2. That is how your first dialog will not be overriden by dialog 2.
Second you can use call back in dialog1, which will be invoked on the input in dialog1, and in your fragment, get that call back and show second dialog.
I'm new to Robotium, I have two questions.
1) I'm trying to make click on custom listview item but its not working. I tried with clickInList(int) and clickInlist(int, int).
2) Handling random AlertDialog:
How to handle display alert dialog dynamically in Robotium? For example I'm using alert dialog when I get any message during call webservice, like connection failure, no internet, server error, timeout, etc..,
Thanks in advance.
There are two important things to note about the clickInList(int) method that aren't readily apparent: First, the list items are 1-indexed, so to click the first item of the list, use clickInList(1) not clickInList(0). Second, the clicking is relative to the visible items on the screen, so clickInList(1) will click the first visible item on the list, not the first item overall.
As for the dynamic handling of a Dialog, arbitrary pop-ups aren't really what Robotium was meant to handle. It's supposed to test user interaction with the app under known, controlled, repeatable conditions. If something unexpected happens in the middle of the test, such as losing connection, it should be considered a failure; There's a good chance your test wouldn't be able to run to completion anyway. As a hacky work-around, you can check for the existence of the Dialog before each of your events, something like:
if(solo.searchText("Dialog text") {
//handle closing dialog
}
However, I'd advise against this, it'll slow down your test considerably, and again, even if you close the dialog, the fact that the error happened in the first place is probably going to cause a later part of your test to fail.
I have created a custom dialog called MyCustomDialog which extends Dialog. I create and show my custom dialog as follows:
new MyCustomDialog(myContext).show();
I override the Dialog.onCreate(Bundle savedInstanceState) method to do my initialisation. I also check in this method whether a certain condition holds and, if not, I would like to dismiss/cancel my dialog. I have tried calling the cancel() and dismiss() methods in my dialog's onCreate(Bundle savedInstanceState) and onStart() methods but it has no effect.
Anyone know how to cancel or dismiss a dialog (from within the dialog) before it shows?
You should place the logic to determine if the dialog is to be shown outside of the onCreate() method. it does not belong there.
Alternatively, rename your show() method showIfRequired() (or something), and add the conditional show logic there.
I know this doesn't technically answer your question, but what you are trying to do is not the correct design. That's a good thing, as doing in the right way is actually simpler.
Also, as a side note, you should using DialogFragment in favor of Dialog. it's available in the v4 support library.
This is for API levels 10 and below:
First you should override onCreateDialog(int id, Bundle args) in the Activity class, is that what you're doing? Dialogs are always created and displayed as part of the Activity. Second, I don't think you can cancel/dismiss a dialog in onCreateDialog because it hasn't actually been created when onCreateDialog is called. That is, you can't cancel/dismiss something that hasn't been created. What you can try is to override onPrepareDialog() instead and do your check to cancel/dismiss the dialog there. At that point the dialog should actually have been created (just not displayed), so you would be able to prevent it from getting displayed if you call cancel/dismiss there.
onPrepareDialog() is the proper place to do any sort of checks and decision making on the dialog that is about to be displayed. This is for APIs prior to Honeycomb.
This is for APIs 11 and later:
If you are using a later API, you should extend DialogFragment instead. In this case I think you can handle the decision making in onCreateView() method of DialogFragment which is similar to onPrepareDialog().
I hope you've read through this:
http://developer.android.com/guide/topics/ui/dialogs.html
or this, depending on your API:
http://developer.android.com/reference/android/app/DialogFragment.html
Overall, perhaps a cleaner solution is to disable the button or mechanism that causes the dialog to show up in the first place? That is, write you code such that Dialog.show() is called only when it really needs to be called. I'd have to know more details about what exactly you're trying to do. For example, say you call Dialog.show() from the onClickListener of a button. you don't really want the user to press a button, expect a dialog, but have it not show up due to some reason the user doesn't understand. A better solution would be to disable the button all together so that it's obvious to the user that this function isn't available due to something else in the application.
I want to launch a dialog from a service that hovers over whatever the user is currently looking at. The dialog gets launched like this: service gets trigger to open dialog > start transparent activity > transparent activity shows dialog.
My problem is when the user opens the app, launches into the main menu, and then presses HOME to leave. By pressing HOME, it leaves the main menu activity on pause, not destroyed, and when the service starts the dialog, the main menu gets shown underneath the transparent activity; causing the dialog to loose the affect of hovering over whatever the user is looking at.
How can make it so that the transparent activity gets opened independently of any other activities in the app? The only way to prevent this currently is to finish all the activities when they are paused; but this is impractical.
This is the last thing we want. :-)
1. Dialog boxes from Services
One of the best experiences in mobile devices, IMHO, and Android in particular, is that after decades, we got rid of system-wide, pesky alert dialogs. Finally, best practices [1, 2] for user interaction gave us a way to avoid the infamous disseminate use of MessageBox(hwnd, lpText, lpCaption, uType), competing for focus and for the attention of the poor user. See video parody above.
The reason it feels awkward to start a dialog from a Service is exactly because it is supposed to be a background task, without user interaction. By concept, you shouldn't be doing this. That's the reason why we see these tricks (transparent activities, what a silly thing) to cheat the design guidelines in the first place. They are bad, they disrupt the user experience, they steal focus and attention. They disrupt our work.
2. Use notifications instead
Whenever you want to notify a user of something from the background, when the user is somewhere else, you use a notification. It's the default pattern, and it doesn't bother the user.
Therefore, you should be sending notifications from your Service.
From there, if the user is interested, then he will touch the notification and you start your own activity, possibly resuming your activity, creating a new one, and then using a dialog requesting action to be performed, or whatever you want to do.
3. Finally, do NOT use FLAG_ACTIVITY_MULTIPLE_TASK
You should not, ever, use this flag unless you have carefully read and fully understood the documentation, and the implications of using that flag.
Do not use this flag unless you are implementing your own top-level application launcher.
(...)
Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.
Really. In this case, just don't.
You may consider using alertDialog with TYPE_SYSTEM_ALERT instead of activity:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
Please note that you have to use the following permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Found this out myself, just add the Intent.FLAG_ACTIVITY_MULTIPLE_TASK flag to the launch Intent; of course in conjunction with the Intent.FLAG_ACTIVITY_NEW_TASK flag.
When is it preferable to use a Dialog box as opposed to an Activity in Android? From my understanding, anything you can do with a Dialog box you can also accomplish with an Activity. Are there any tasks that can only be accomplished by an Activity or a Dialog?
Is what you're doing worth a new Activity? Do you need to be able to start it through an intent? Do you really need to create a new Java class for it?
If it's a straightforward dialog that displays a text and has simple hooks for positive/negative/dismissal functions, definitely use a dialog.
If you have something complex, you may want to go for a full-blown activity.
Well why exactly would you want to start a new activity just to ask the user "Are you sure? Y/N"? Dialogs generally run on top of the activity, and are usually smaller activities or notifications for the user. They also usually have something to do with the process of the app running. It helps make things simpler to open a dialog to prompt the user on top of your activity, than to start a new activity atop your current activity.
I went for Activities when I needed an user interaction that needs backstack, navigation, lifecycle and callable features.. else with dialogs. Being from the WebApp world I ask whether I would have needed a new server page or a pop window for an interaction and the decision in Andoird world becomes easier!
If newServer page then mostly Activity
elseIf popUpWindow then dialog
I created my android application in one fragment with nested alert dialog, so far my application still running well in handle those nested dialog, and I think dialog is less consuming memory than an activity