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

  • SEARCH
  • Home
  • 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 1054905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:28:47+00:00 2026-05-16T17:28:47+00:00

ProgressDialog quits updating when orientation of screen changes. I have put into place a

  • 0

ProgressDialog quits updating when orientation of screen changes. I have put into place a fix that salvages the asynctask and sets the activity of the asynctask to the new activity after it is destroyed and rebuilt. The percentage complete on the progressdialog stays at the percentage it was at before the orientation change.

What am I missing?

package net.daleroy.fungifieldguide.activities;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Toast;
import net.daleroy.fungifieldguide.R;
import net.daleroy.fungifieldguide.fungifieldguideapplication;

public class FungiFieldGuide extends Activity {
    //static final int PROGRESS_DIALOG = 0;
    //ProgressThread progressThread;
    private final static String LOG_TAG = FungiFieldGuide.class.getSimpleName(); 
    fungifieldguideapplication appState;
    private DownloadFile mTask;
    public boolean mShownDialog;
    ProgressDialog progressDialog;
    private final static int DIALOG_ID = 1; 

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);

        if ( id == DIALOG_ID ) {
            mShownDialog = true;
        }
    }

    private void onTaskCompleted() {
        Log.i(LOG_TAG, "Activity " + this + " has been notified the task is complete.");

        //Check added because dismissDialog throws an exception if the current
        //activity hasn't shown it. This Happens if task finishes early enough
        //before an orientation change that the dialog is already gone when
        //the previous activity bundles up the dialogs to reshow.
        if ( mShownDialog ) {
            dismissDialog(DIALOG_ID);
            Toast.makeText(this, "Finished..", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case DIALOG_ID:
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("Loading Database (only first run)...");
            return progressDialog;
        default:
            return super.onCreateDialog(id);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        appState = ((fungifieldguideapplication)this.getApplication());

        Object retained = getLastNonConfigurationInstance();
        if ( retained instanceof DownloadFile ) {
            Log.i(LOG_TAG, "Reclaiming previous background task.");
            mTask = (DownloadFile) retained;
            mTask.setActivity(this);
            //showDialog(DIALOG_ID);
        } 
        else {
            if(!appState.service.createDataBase())
            {
                Log.i(LOG_TAG, "Creating new background task.");
                //showDialog(DIALOG_ID);
                mTask = new DownloadFile(this);
                mTask.execute("http://www.codemarshall.com/Home/Download");
            }
        }
            //showDialog(PROGRESS_DIALOG);

        View btn_Catalog = findViewById(R.id.btn_Catalog);
        btn_Catalog.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {   
                Intent i = new Intent(getBaseContext(), Cat_Genus.class);//new Intent(this, Total.class);
                startActivity(i);
            }
        });

        View btn_Search = findViewById(R.id.btn_Search);
        btn_Search.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {   
                Intent i = new Intent(getBaseContext(), Search.class);//new Intent(this, Total.class);
                startActivity(i);
            }
        });
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
            mTask.setActivity(null);
            return mTask;
    } 

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        //progressDialog.dismiss();
        //progressDialog = null;
        appState.service.ClearSearchParameters();
    }

    private class DownloadFile extends AsyncTask<String, Integer, Boolean>{
        private FungiFieldGuide activity;
        private boolean completed;
        private String Error = null;
        private String Content;

        private DownloadFile(FungiFieldGuide activity) {
            this.activity = activity;
        }

        @Override
        protected void onPreExecute()
        {
            showDialog(DIALOG_ID);
        }


        @Override
        protected Boolean doInBackground(String... urlarg) {
            int count;

            try {
                URL url = new URL(urlarg[0]);
                URLConnection conexion = url.openConnection();
                conexion.setDoInput(true);
                conexion.setUseCaches(false);

                // this will be useful so that you can show a tipical 0-100% progress bar
                int lenghtOfFile = conexion.getContentLength();

                // downlod the file
                InputStream input = new BufferedInputStream(conexion.getInputStream());
                OutputStream output = new FileOutputStream("/data/data/net.daleroy.fungifieldguide/databases/Mushrooms.db");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    publishProgress((int)total*100/lenghtOfFile);
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                Log.i(LOG_TAG, e.getMessage());
            }
            return null;
        }

        @Override
        public void onProgressUpdate(Integer... args){
            progressDialog.setProgress(args[0]);
        }

        @Override
        protected void onPostExecute(Boolean result)
        {
            completed = true;
            notifyActivityTaskCompleted();
        }

        private void notifyActivityTaskCompleted() {
            if ( null != activity ) {
                activity.onTaskCompleted();
            }
        }

        private void setActivity(FungiFieldGuide activity) {
            this.activity = activity;
            if ( completed ) {
                    notifyActivityTaskCompleted();
            }
        }
    }
}
  • 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-05-16T17:28:48+00:00Added an answer on May 16, 2026 at 5:28 pm

    This is not a real solution but to prevent this I just disabled orientation changes during the life of the AsyncTask with adding first:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

    and when the job is done:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

    Hope this helps.

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

Sidebar

Related Questions

I have a ProgressDialog running in a AsyncTask. I`m trying to achive that as
I have code that starts a ProgressDialog inside an AsyncTask , it looks like
I'm beginning to think that to get a ProgressDialog to work the AsyncTask has
I'm having some problems displaying a ProgressDialog. I have a method that scrapes information
My program uses wx.ProgressDialog to give feedback on a process that is in multiple
I have the next code ProgressDialog pd; LinearLayout layout; ListView listview; TextView name[]; TextView
I want to make a ProgressDialog for my app that used flashes an image
I am trying to display an indefinite ProgressDialog, while an AsyncTask binds to a
I'm trying to make a simple ProgressDialog appear while my AsyncTask is fetching data.
I show ProgressDialog in AsyncTask method. My code as below: @Override protected void onPreExecute()

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.