Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment misses broadcasts while being restarting
    primarykey
    data
    text
    <p>On Android, I have implemented a class <code>AsyncTaskWithProgress</code> which shall show a progress dialog while the task's background work is being performed:</p> <pre><code>public abstract class AsyncTaskWithProgress&lt;Params, Progress, Result&gt; extends AsyncTask&lt;Params, Progress, Result&gt; { private final FragmentActivity activity; private final String action; private final String title; private final String message; private final int icon; public AsyncTaskWithProgress(FragmentActivity activity, String title, String message, int icon) { this.activity = activity; this.title = title; this.message = message; this.icon = icon; this.action = "" + hashCode(); } @Override protected final void onPreExecute() { DialogFragment fragment = ProgressDialogFragment.getInstance(action, title, message, icon); fragment.show(activity.getSupportFragmentManager(), "dialog"); actualOnPreExecute(); } protected void actualOnPreExecute() { } protected final void onPostExecute(Result result) { actualOnPostExecute(result); context.sendBroadcast(new Intent(action)); } protected void actualOnPostExecute(Result result) { } } </code></pre> <p>My <code>ProgressDialogFagment</code> registers a <code>BroadcastReceiver</code> and cancels the dialog as soon as the broadcast has been received - excerpt:</p> <pre><code> public static Dialog getDialog(Activity activity, final String action, String title, String message, int icon, SerializableOnClickListener cancelListener) { final ProgressDialog dialog = new ProgressDialog(activity); dialog.setIndeterminate(true); dialog.setOwnerActivity(activity); dialog.setTitle(title); dialog.setMessage(message); if (cancelListener != null) { dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "Cancel", cancelListener); } if (icon != MathUtils.NOT_AN_INT) { dialog.setIcon(icon); } BroadcastReceiver dismissReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { dialog.dismiss(); context.unregisterReceiver(this); Log.d(getClass().getSimpleName(), "Dialog dismissed"); } }; context.registerReceiver(dismissReceiver, new IntentFilter(action)); return dialog; } </code></pre> <p>This works in general. However, every once in a while the dialog does not get canceled, and it seems that there are orientation changes involved.</p> <p>My guess is that during orientation change, the dialog fragment gets destroyed and reinstantiated. If the AsyncTask sends its "Finished!" broadcast after the fragment got destroyed and before it got reinstantiated, the fragment misses the broadcast and thus never dismisses.</p> <p>Is my guess right? How to implement this in a more reliable way?</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.
    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