Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:18:14+00:00 2026-06-17T21:18:14+00:00

On Android, I have implemented a class AsyncTaskWithProgress which shall show a progress dialog

  • 0

On Android, I have implemented a class AsyncTaskWithProgress which shall show a progress dialog while the task’s background work is being performed:

public abstract class AsyncTaskWithProgress<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

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) {
}

}

My ProgressDialogFagment registers a BroadcastReceiver and cancels the dialog as soon as the broadcast has been received – excerpt:

    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;
}

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.

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.

Is my guess right? How to implement this in a more reliable way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:18:16+00:00Added an answer on June 17, 2026 at 9:18 pm

    Since you’re using fragments, I would suggest instead using setRetainInstance() to make a fragment that persists across configuration changes. You could register your broadcast receiver from that fragment, show or hide the progress, etc. (Note that a fragment does not need to be visible to the user.)

    Another approach that works without fragments is to add a standalone BroadcastReceiver class (registered in your manifest) for that intent, and save a value to preferences to alert your application that the broadcast was received.

    In fact, you could skip the standalone BroadcastReceiver and just save a value to preferences from your AsyncTaskWithProgress that indicates the current state of the operation, e.g. Status.LOADING, Status.LOADED, Status.CANCELLED, etc. and check that value in onCreate(). However, I think using setRetainInstance() is the best approach since you’re already using fragments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Android and Java programming. I have a class which implements
I have made an android app which working fine. I implemented the Login functionality
I have implemented an HTTPOperations class in Android that can successfully produce an HTTP
I have implemented one application in android which uses epublib to view .epub files.
For an Android application, I have implemented an external function in C, which I
I have the following class to show some images... I have implemented an ability
I have implemented a thread in android which refresh the fragment (some text list
I have implemented onRetainNonConfigurationInstance() on one of my Activity to handle android screen orientation.
I have been trying to implement an upgrade plan for my Android app which
Building an Android application using Google's GCM service. I have implemented the onRegistered method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.