I have a custom dialog and despite putting in all the code to inflate the view in its constructor, it seems like it takes much longer when the dialog is first launched compared to subsequent launches, as if only when i call dialog.show(); it actually creates it. How could I do what I intended, to properly preload the dialog to prevent this first run delay?
You can Extend your class with AsyncTask<> and perform all work in doInBackground().in onPreExecute() Show Dialog and in onPostExecute() remove the dialog from UI.
Related
I am having one java class in that class as soon some one purchases our application then it will start downloading and progress dialog has to appear instead it goes to some other page and when i come out of the application and when i restart then it starts downloading.
Please Help me out from this mess...
Thank you
Check the condition for dialog, before showing.
Like this
if(pDialog!=null)
{
if(!pDialog.isShowing())
{
pDialog.show();
}
}
and also while removing the dialog in onPostexecute() check for null.
if Still not works just remove the pDialog and try once with your code.
Two causes for your error happen:
The error will happen if you're trying to show a Dialog after you've exited an Activity.
Also, if an unhandled Exception was thrown in your AsyncTask, which would cause the Activity to shutdown, then an open progress dialog will cause the Exception.
According to the Log you've posted, the error happens after you call pDialog.show() which might be the 1st cause I've mentioned before.
Also you are calling finish() in many parts of your code, maybe one of these calls are making your Activity to stop and leaking your Dialog.
You must check which one of them is finishing your Activity before you show the Dialog. A good solution is to dismiss the Dialog (if it's showing) before calling finish().
I'm calling an AlertDialog that exists in a different class. Directly after, I use a callback method to refresh the view. However, it appears the dialog runs on a separate thread, because the callback method gets called before the user makes a choice in the AlertDialog, instead of after.
How do I make sure the callback method is only called after the user has made a choice?
mClientManager.deleteClientConfirmationDialog ( getActivity (), id );
mCallback.refresh ();
However, it appears the dialog runs on a separate thread
No, it does not.
because the callback method gets called before the user makes a choice in the AlertDialog, instead of after
The dialog is not being shown until well after deleteClientConfirmationDialog() and refresh() have been called. You may be requesting the dialog to be shown somewhere in there, but all that does it put a message on a queue, to be processed by the main application thread, sometime after you return control of that thread to the framework.
How do I make sure the callback method is only called after the user has made a choice?
Put the logic in an appropriate event listener. Lance's suggestion of setOnDismissListener() is a reasonable choice, if you want refresh() to be called regardless of how or why the dialog is going away.
Progress dialog freeze when adding a large view in layout from onPost of AsyncTask. We have to add a dynamic layout so we have called that from onPost,( On calling that from doInBackground error occure, So we have called that from opPost) But now the progress dialog freeze when loading that layout.
Please Help...
I have created a separated class and called that class method
protected void onPostExecute(Void unused)
{
eLayout=new ExcelLayout(viewScreenActivity);
eLayout.LinearLayoutXLSView(ROW_HEIGHT, COL_WIDTH, ROW_Title_Width,NUM_COLS, NUM_ROWS, cols, rows, data);
progressdialog.dismiss();
}
Progress dialog freeze and disappear after task completed.
This problem raises when there is a huge process such as drawing a view happens in the main UI thread. The progress dialogs are very light weight process and when some other process takes priority then they just stop there without rotating (in simple) which makes it looks like it has hanged. But the actual reason is, there is some other process which takes more priority than showing the progress dialog.
In your case it is the View creation which takes priority. So what you have to do is, dismiss the dialog before starting to draw the layout.
But if you are looking for a way to show a progress dialog while you draw your view i am afraid it is not possible, from my experience so far.
EDIT
it is very much clear that your main UI does a lot of work. YOu can try this but I am not sure how far this will work. Instead of just calling the methods immediately after the progressdialog.dismiss(), you could use the dismiss listener and call the methods from there. For example,
progressDialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
eLayout=new ExcelLayout(viewScreenActivity);
eLayout.LinearLayoutXLSView(ROW_HEIGHT, COL_WIDTH, ROW_Title_Width,NUM_COLS, NUM_ROWS, cols, rows, data);
}});
So this gets called only when the progress dialog's progressdialog.dismiss(); is called. So it should work probably.
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.
Let's say we have two buttons, each with a OnClickListener. Each of listeners show a ProgressDialog and do some background work. (Behind the scene is an AsyncTask, the dialog is opened in onPreExecute. I don't think it matters, just for the record...). Let's say there is some rule saying no more than one background worker may be active at any given time.
My assumption was that the Dialog prevents two background workers running at the same time. I thought the modal dialog blocks the UI and it's not possible to click another button after the show() method of the dialog is called. I was wrong.
If you click the buttons fast enough, it's possible to trigger both background workers (almost) at the same time. The log shows that it's possible to click two Buttons within a 150 ms time span despite the Dialog:
04-14 18:34:04.390: DEBUG/greenrobot(1860): Clicked: 2131034112
04-14 18:34:04.470: DEBUG/greenrobot(1860): doInBackground2: 2131034112
04-14 18:34:04.540: DEBUG/greenrobot(1860): Clicked: 2131034113
04-14 18:34:04.570: DEBUG/greenrobot(1860): doInBackground2: 2131034113
The dialog code looks like this:
progressDialog = new ProgressDialog(currentActivity);
progressDialog.setMessage(msg);
progressDialog.show();
What did I miss? I hope I missed something really stupid, because if not, I cannot think of a nice and solution preventing UI interaction after the click. Synchronizing the background workers is not a solution because the UI and scenario is more complex.
Disable the button after it is clicked, until it is safe to be clicked again.
show() is asynchronous. The dialog will not appear immediately upon the call to show(). It will occur moments later.