Note that there are some explanatory texts on larger screens.

plurals
  1. POProgressdialog not dimissing
    primarykey
    data
    text
    <p>My code tries to do this:</p> <ul> <li>onCreate it creates and instance of an asynctask A</li> <li>onPreExecute of A we make call: showDialog</li> <li>in doInBackground we sleep for a while SystemClock.sleep(1000)</li> <li>onPostExecute we dismiss the dialog then start a new Async Task B</li> <li>Task B does mostly the same by opening a dialog waiting then closing it.</li> </ul> <p>Now when you first open the app things happen as the should: you see two Progress dialogs pop up one after the other, each for a period of time.</p> <p>However if I rotate the screen at anytime, the next time the activity is created the <strong>progress dialogs are created but never dismissed</strong>. which makes it impossible to use the UI. If I rotate the screen again, I get an error message about leaked views (the dialogs that dint go away). plus new dialogs are now created and also don't want to go away (be dismissed).</p> <p>here my code:</p> <pre><code>public class MainActivity extends ListActivity { private static final int REFRESH_DIALOG = 100; private Resources resources; @Override protected void onCreate(Bundle savedInstanceState) { resources = getResources(); setContentView(R.layout.listview); new CreationWork(this).execute(); super.onCreate(savedInstanceState); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case REFRESH_DIALOG: String message = resources.getString(R.string.refreshing); ProgressDialog refreshDialog = new ProgressDialog(this); refreshDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); refreshDialog.setMessage(message + "..."); refreshDialog.setCancelable(false); return refreshDialog; default: return null; } } private static class CreationWork extends AsyncTask&lt;Void, Void, DatabaseManager&gt; { private MainActivity activity; public CreationWork(MainActivity activity) { this.activity = activity; } @Override protected void onPreExecute() { activity.showDialog(MainActivity.REFRESH_DIALOG); super.onPreExecute(); } @Override protected DatabaseManager doInBackground(Void... params) { SystemClock.sleep(10000); return null; } @Override protected void onPostExecute(DatabaseManager result) { activity.dismissDialog(MainActivity.REFRESH_DIALOG); new RefreshWork(activity).execute(); super.onPostExecute(result); } } static class RefreshWork extends AsyncTask&lt;Void, Void, Cursor&gt; { private MainActivity activity; public RefreshWork(MainActivity activity) { this.activity = activity; } @Override protected void onPreExecute() { activity.showDialog(MainActivity.REFRESH_DIALOG); super.onPreExecute(); } @Override protected Cursor doInBackground(Void... params) { SystemClock.sleep(1000); return null; } @Override protected void onPostExecute(Cursor result) { activity.dismissDialog(MainActivity.REFRESH_DIALOG); super.onPostExecute(result); } } } </code></pre> <p><em>Some debug info:</em></p> <p>There are no errors untill a second rotation. which makes sense beacuse if the progress dialogs aren't dismissed between the first and second rotations, they are effectively leaked.</p> <p>when the app first runs, despite two calls to showDialog, onCreateDialog is called only once (I believe this is the normal behaviour). however after the first rotation and for any subsequent rotation onCreateDalog will be called twice per run.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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