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.
Related
I have an Activity that shows different kinds of Dialog; some are AlertDialog that can only be dismissed by the user others are ProgressDialog that monitor an AsyncTask and some are CustomDialog.
My question is what happens if Android decide to destroy my Activity and one of these dialog is shown? (I have nothing in onDestroy that dismiss the dialog because there are too many and in different classes.) Will the Dialog gets dismissed or I end up with a crash?
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 have an AlertDialog which appears initiated by a BroadcastReceiver - so the AlertDialog may appear ontop of ANY of my activities without knowing which activityis actually under it.
private void showAlert(Context context, String s) {
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.create();
alertDialog.setTitle(context.getString(R.string.SMS_Alert_title));
alertDialog.setMessage(s);
alertDialog.setButton(context.getString(R.string.alert_OK),
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; //can't call the underlying activity's ui update method bec I don't know which activity is actually underlying
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
When I now press the "OK" button on the AlertDialog to dismiss it, the pressed status of all my buttons in the underlying activity become unpressed. I need them to remain pressed simply because my buttons show a different png during pressed status - which I also use to show that they are and remain "ON" when pressed. (I can't use toggle buttons in this case)
Ideally I just need to update my UI of the underlying activity but onResume is NOT called when the AlertDialog gets dismissed.
Also I cannot call any UI update method when pressing the ALertDialog OK button, since I do not know which activity is actually under the AlertDialog (as the ALertDialog may appear ontop of any activity)
(I hope I could explain the problem well enough)
ps While I could change the background of the unpressed buttons to the pressed png instead of just saying btn.setPressed(true) I would like to avoid it.
Many thanks
I'm not quite sure what you're trying to achieve. But have a go at following.
Have an interface named... say 'PressableActivity'? which has one method. 'pressAllButtons'
Implement this on all your activities you want the explained functionality, and implement the method to press all buttons when called.
Have a variable of type 'PressableActivity' on your context (or even a static class and a static variable will do.)
Assign this variable to the activity being displayed when it gets a call to onResume.
When you create the dialog, set an onDismissListener to it, which calls the 'pressAllButtons' method on the object pointed by static variable on your context.
Hope this helps.
One way or another you're going to need to "know" which activity is being displayed after the AlertDialog is dismissed. You can set an onDismissListener on your AlertDialog inside of your Activity and then respond accordingly. Why you would want your buttons to remain in a pressed state after they are not pressed is beyond me, but if that's what you really want then just set the state to pressed since that's essentially what you want: a forced pressed state even though the user hasn't pressed it again.
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).
I'm trying to setup UI where user will have to login first time they use applications. And Custom Dialog seems like a good thing to use as I want main UI to be kind of visible on background.
So, what I did - I created main Activity and use ShowDialog() with onCreateDialog from main activity.
I created
public class LoginDialog extends Dialog implements View.OnClickListener
and I can control all stuff on dialog just like activity.
Ideally I like to check if user logged in on main activity and if not - show this dialog. Otherwise just go with activity.
On dialog I'd like to log user in and if user clicks back without logging in - I want to detect it and just finish main activity. This is where I have problem.
In WinForms I would do ShowDialog() (in C#) and next line executed when dialog closed for any reason. I can't figure out how to do this in Android.
I didn't get to it yet, but I want to show progress bar when Login button clicked. This bar will be in Dialog box. Is it possible/doable?
Thanks in advance!
You can detect dialog dismissal using setOnDismissListener. In this method you could also call MyActivity.this.finish().
For showing a ProgressBar during the login process, you probably want to look at this answer. It shows the basic structure of AsyncTask and you can adapt it to use ProgressBar instead of ProgressDialog.
You would be changing the bar's visibility in onPreExecute and onPostExecute with bar.setVisibility(View.VISIBLE) and bar.setVisibility(View.INVISIBLE).
Edit
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
if (!isLoggedIn()) {
MyActivity.this.finish();
}
}
});
This code should be in your MyActivity wherever you create the dialog. You need to check to see if the user is logged in or not, because onDismiss will be called whether it's the user or your own code that closes the dialog.