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).
Related
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 asynctask running, when finish, I dismiss the progressDialog and call AlertDialog accordingly:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
Standy on app
Standby on app
Asynctask on going
Asynctask is finished, progressDialog is dismissed
(Still standby on app) AlertDialog appears as it should be
Home screen
Asynctask on going
Go to home screen
Asynctask is finished, progressDialog is dismissed
AlertDialog is called to show
Goes back to app, but alertdialog doesn't appear, it gives me greyed screen instead.
Is there any way to solve this? I tried to search it but couldn't find someone has same issue..
Ok. (As from your comments).
You can not have your dialog automatically appear while your app is in the background. The dialog is displayed within the context of your Activity(along with its window manager), if the activity is not displayed, nor is your dialog.
To display content while your activity is in the background you have a few options, non of which will be straight forward though.
Attach views directly to Android's WindowManager. This will not work with a dialog, you would have to construct a dialog layout and manually attach it, it would remain foreground above everything else.
Launch your dialog from a transparent activity. Problem here is if you are say on your homescreen then the transparent activity/dialog launches and you then call you application the transparent activity/dialog will be dismissed into the background.
Launch an activity with a dialog theme. This "may" work as needed, if you call your main application keeping the stack correct. Would have to test to be sure.
According to the Android Design Guide you should show a Notification instead of the AlertDialog when your AsyncTask has finished and the Activity is in background (paused). To decide what you should show, maintain a boolean variable in your Activity. Set the variable to true inActivity.onResume() and false in Activity.onPaused().
Basically you implement onPostExecute in your AsyncTask. In there you make the ProgressBar invisible and show the Alert Dialog with the code you show in your question.
More about AsyncTask is here
More about Notifications is here
The Design Guide in question is here. Scroll down to 'Sending Notifications to the User'. Essentially, what you do with the AsyncTask in background is like running a Service. At least from the user experience standpoint. Differences between your approach and a Service are internal, e.g. Android will kill your process when it needs space differently.
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
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 have the following problem:
I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back.
At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.
This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.
How can I achieve a dialog that is only shown if the activity is still active?
Call isFinishing() on your activity.