Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please be aware that every time you change the orientation, a new instance of your <code>Activity</code> (and consequently of your <code>Dialog</code> via <code>onCreateDialog</code>) is created. You can verify this by adding a log statement in either constructor.</p> <p>Although difficult to say without having a glance at your code, I'm guessing you're calling <code>dismissDialog</code> on an older instance of your <code>Activity</code>.</p> <p>Consider the following <code>Activity</code> that simply shows an empty <code>Dialog</code> when a button is clicked and starts a <code>TimerTask</code> to dismiss it after 10 seconds:</p> <pre><code>public class Test extends Activity { private Dialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); Button btn = (Button) findViewById(R.id.testButton); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Timer().schedule(new TimerTask() { @Override public void run() { dialog.dismiss(); } }, 10000); Test.this.showDialog(1); } }); } @Override protected Dialog onCreateDialog(int id) { if(id == 1) { if(dialog == null) { dialog = new Dialog(this); } return dialog; } return super.onCreateDialog(id); } } </code></pre> <p>This code has a problem similar to the one you're describing: If - during the 10 seconds - the orientation is changed, the <code>Dialog</code> is never dismissed. <em>Why?</em> Because at the time the <code>TimerTask</code> is <code>run</code>, a whole different set of <code>Test</code> and <code>Dialog</code> instances has been created (during orientation change) and the <code>TimerTask</code>'s <code>dialog</code> instance variable references a <code>Test</code> instance that is different from the one that is currently active on the screen.</p> <p>Obviously there are several ways to deal with this issue (mostly depending on your application), one quick (dirty?) way to fix the above code would be to simply make the <code>dialog</code> field <code>static</code>:</p> <pre><code>private static Dialog dialog;</code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload