I have a network monitor (using Broadcast receiver) in my app, what it does it pops an alertdialog if the phone is not connected to internet and if the phone connects back it pops another alert informing the user he is back online. The problem is every time the monitor sees a state change it creates another window that stacks on top of the other one. For example if the phone has its DATA ON/OFF 3 times it will pop 6 messages. Is there a way of dismissing an alert dialog if another one opens or what would be the best approach to overcome this scenario?
Thank you in advance, your help is very much appreciated.
Android newbie
Add a variable containing the currently visible dialog:
Alertdialog a;
and then when you want to show one:
if (a!=null) {
a.cancel();
}
a = yourDialogBuilder.show();
Hope this helps.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
I think you should consider re-designing your notification mechanism. Using Dialog can be very disturbing to the user experience since it requires an action to dismiss and interrupt the natural flow of the app.
Consider using Toast for simple notifications or Notification if you want it to show in the statusbar. Also, you can have some icon in your application that reflects the current connectivity status e.g. green is connected and red otherwise.
To ensure only one dialog is available, you can create a static instance of your dialog in your activity and dismiss it if it is not null before showing a new dialog
Related
I want to show an Dialog Whatever the screen the User is in. Suppose if user opens application and in initial screen if I receive a server message I have to show it in a dialog.Meanwhile there is an option of autologin . So it could move to my next activity.If this is the case that dialog should not be closed.It should show on newly opened activity rather than the previous activity.And other thing is that even though the dialog is shown I should be able to control my buttons on the activity.
Here's what i am doing.
if(Activity1.mcontext!=null){
CommonMethods.showDialog(sliderMessageText,
LoginActivity.mcontext,"activity1");
}
if(Activity2.context!=null){
CommonMethods.showSliderMessageText(sliderMessageText,
Activity2.context,"activity2");
}
if(Activity3.mcontext!=null){
CommonMethods.showSliderMessageText(sliderMessageText,Activity3.context,"activity3");
}
Instead I am displaying the dialog in all the activities.
Thanks in advance.
You may want to consider using an Event driven model such as GreenRobot.
http://greenrobot.org/eventbus/
This would allow you to efficiently handle the scenario you describe.
Alternatively, you can use the LocalBroadcastManager to communicate between different parts of your app.
e.g Send a broadcast message when you want to display a dialog and handle that message in all of your Activities
I have an app with multiple Activity classes. In one part, one is a main screen and the next is a login. The user would open the login screen and enter their info and press a button to submit it. The resulting code opens up an AlertDialog to tell them the registration status, along with some other information. If the registration itself was successful, they will return to the previous activity. But this makes any AlertDialog disappear with it.
I want the background to return to the previous activity so they can see the home page start updating, but I want them to see the alert message long enough to read it.
Yes this is possble. By using a service you can display an alertbox which runs in foreground for a long time. I have done thissame kind of thing for a news app.
It will run in foreground for a long time and when you want to switch you can stop the service than it will dismiss the alert box-.
In my case I displayed a custom view with buttons in the foreground.
I have an AlertDialog appear whenever I find that the device location has changed. This happens pretty much at anytime the application sees fit.
The problem is that if the dialog appears just before a new Activity is started the dialog disappears with the old Activity. Is there anyway to have the AlertDialog block new Intents? Or translate over to the new Activity (could I update the Context)?
I noticed that AlertDialogs don't even handle the case where the device rotates, so my hope for this isn't too high, but any input would help.
I would simply use a SharedPreference setting and check it whenever your potentially interruptive Activity starts.
Ex;
set shared preference value to 1
start dialogue
when finished, save the shared preference to 0
if activity starts and shared preference is 1, it interrupted so start an alert dialogue. if it is 0, move on.
Again, an AlertDialog is meant to attach itself to an activity, so you really can't stop it from dieing, if your previous activity is pushed back or loses focus of the screen (ui thread basically put on hold!)
Hope you can fix your problem with this minor fixup.
To add what Daniel commented, Custom toasts can be awesome. They are, however, limited to about 3.5 seconds (I think)
More on Custom Toasts; http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
Your question is not clear for me,As I understood you need to show a Alertdialog whenever the device location is changed that too above a new activity.My suggestion is that when ever the device location changes get the need values,bundle it with the intent and pass it starting a new activity.With the bundle also pass a boolean flag.If the boolean flag is true in the new activity show a Alertdialog box in onCreate,I hope you will get what you wanted.This will be the best scenario for you to do this as I understood.Also keeping the history of these activity's may affect the app while implementing this.
I am running a service as soon as application starts. That service will keep checking SQLite for data. If it finds a certain data, a dialog box should pop up (user can be on any activity). Don't want to use notification. How do I pop up Dialog box from a service ?
Short question is you cant. However, you can send an intent to your activity for it to be the one showing the dialog box, or you can start an activity that looks like a dialog.
I write a program support download file. When in this download activity, I start a progress bar and then run a thread to do the download things. This thread send message to UI thread to notify how many has been downloaded frequently. In the main(UI) thread, I update the progress bar display when receive the message. If any problem happen in download progress, it will send another message. When main thread receive the message, it stop the progress bar and pop up a new AlertDialog to show the error reason.
Here is a special test for it. When downloading, switch the program to settings. Turn off the WIFI/GPRS to make the network off.
When I back to my program, it should display the background ui and a pop up AlertDialog to show the reason as I wish. But it only display the background ui(which means the main activity) and with a half-light of backlight just as the popup windows still there. When I press back for first time, nothing happens except the backlight is bright just like I have close a pop up window.
I think maybe it is because when I start the AlertDialog my activity is not in foreground.
I tried to use:
ActivityManager am = (ActivityManager)Update.this.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
to judge whether the top activity is mine. But I want to show the dialog. If the activity isn't being seen by the user, when he gets back to my activity, I want it to show the dialog.
Indeed the dialog already shown but not visible to the user and it would become visible if you rotate the device.
There seemed a refresh/drawing issue if dismiss a progress dialog and show another new dialog immediately when the activity is not in foreground.
During my testing, such issue not happen if wait for the dismiss action finished for the progress dialog and then show the new dialog.
So one solution is that show the AlertDialog first and then dismiss the ProgressDialog. That worked for my application.
I think, there is some king of bug in Dialog. I have the same situation, and only solution was dismiss curent dialog by dialogDismiss(id) and show it again by showDialog(id).