Reputation: 595
public class myActivity extends Activity
{
private static AlertDialog somedialog = null;
protected void onCreate(Bundle savedInstanceState)
{
somedialog = new AlertDialog.Builder(ctx).create();
innerclass = new innerclass();
innerclass.start();
}
private class innerClass extends Thread
{
if (!somedialog.isShowing())
{
runOnUiThread(new Runnable()
{
somedialog.setMessage("test");
somedialog.show();
}
}
}
}
I have this code where I have a dialog which needs to show only if the dialog is not already there. The dialog is only shown by the innerClass. There is no other method which invokes this somedialog object.
I expect the dialog to appear for the first time and to reappear only if the previous dialog has been closed, however I find there are multiple dialogs stacked on.
For some reason the isShowing method is returning false even if the dialog is open.
Any ideas as to why this is happening?
However when this code is executed somedialog.isShowing() is always returning false and hence I get multiple dialogs on top of each other.
Upvotes: 3
Views: 1420
Reputation: 595
Thanks for everyone who tried to help me out in this query. It finally turns out to be silly coding error @ midnight. I have multiple runOnUiThreads inside the innerclass thread, in one of them the alert object was being re-created.
Upvotes: 1
Reputation: 8231
It's possible there are some issues introduced by your multi-threading, and the value of isShowing()
being cached as it is read between threads. I can't see the purpose of innerClass
from your example, and would simply remove it. This would also be akin to:
private class innerClass extends Thread {
runOnUiThread(new Runnable() {
if(!someDialog.isShowing()) {
somedialog.setMessage("test");
somedialog.show();
}
}
}
Also, you shouldn't keep a static
reference to your AlertDialog
- there's no need, it wont play nicely with the lifecycle of your Activity
, and it's a memory leak waiting to happen.
Upvotes: 0
Reputation: 7480
You are creating a new instance of AlertDialog every time
new AlertDialog.Builder(ctx).create();
and checking isShowing()
before it showing. As a result it always return false.
Upvotes: 2