Freddy Daniel
Freddy Daniel

Reputation: 389

Progress Dialog impossible to show using one AsyncTask class

I'm trying to show one Progress Dialog inside my MainActivity but is impossible: This class is inside My MainActivity:

   class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

        ProgressDialog progressDialog;
        private String titulo;
        private String mensaje;
        private MainActivity activity;
        public MyAsyncTask(MainActivity activity){
            this.activity = activity;

        }

        @Override
        protected Void doInBackground(Void... voids) {
            this.activity.createDirs();
            this.activity.copyAssets();
            return null;
        }

        @Override
        protected void onPreExecute() {

            this.progressDialog = new ProgressDialog(MainActivity.this);
            this.progressDialog.setTitle("test");
            this.progressDialog.setMessage("test message...");
            this.progressDialog.show();
            Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
        }

    }

This code is inside My MainActivity.java: Basically when the user press one button I'm creating one reference to my MyAsyncTask and executing it.

btnInit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag==1){
                    myAsyncTask = new MyAsyncTask(new MainActivity());
                    myAsyncTask.execute();
                    try {
                        myAsyncTask.get();
                    }catch (Exception e){

                    }

                    Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
                    startActivity(intent);
                    flag=0;
                }else{
                    Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();

                }
            }

The code of the method doInBackground() of the class MyAsyncTask is being executed without problems. I will appreciate any idea guys, thanks so much.

Upvotes: 2

Views: 62

Answers (1)

Shay Kin
Shay Kin

Reputation: 2657

Your thread is blocked, when you call myAsyncTask.get() that's why your Progress Dialog is not displayed so remove the myAsyncTask.get() and add those line on onPostExecute

Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
                    startActivity(intent);

full code :

  btnInit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(flag==1){
                        myAsyncTask = new MyAsyncTask(new MainActivity());
                        myAsyncTask.execute();
  
                        flag=0;
                    }else{
                        Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
    
                    }
                }

and on your asyncTask :

   class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

        ProgressDialog progressDialog;
        private String titulo;
        private String mensaje;
        private MainActivity activity;
        public MyAsyncTask(MainActivity activity){
            this.activity = activity;

        }

        @Override
        protected Void doInBackground(Void... voids) {
            this.activity.createDirs();
            this.activity.copyAssets();
            return null;
        }

        @Override
        protected void onPreExecute() {

            this.progressDialog = new ProgressDialog(MainActivity.this);
            this.progressDialog.setTitle("test");
            this.progressDialog.setMessage("test message...");
            this.progressDialog.show();
            Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
            Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
            startActivity(intent);
        }

    }

Upvotes: 1

Related Questions