filipp.kowalski
filipp.kowalski

Reputation: 5635

Progress dialog in AsyncTask do not show up

I know there was similiar problem to this, but I still haven't found an answer. The problem is that progress dialog for this long operation won't show up, but still process is being done. I think there is problem with the context, but dunno how to solve this.

public class MainActivity extends Activity {

Utilities uti = new Utilities();
SharedPreferences prefs = null;
private ContactServiceActivity contactService;
ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactService = new ContactServiceActivity(getApplicationContext());

    doFirstRun();

    Intent i = new Intent(getBaseContext(), ContactListActivity.class);
    startActivity(i);
}

private void doFirstRun() {
    SharedPreferences settings = getSharedPreferences("pl.stxnext.stxcontactsync", MODE_PRIVATE);
    if (settings.getBoolean("isFirstRun", true)) {

        new firstRunTask().execute();

        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("isFirstRun", false);
        editor.commit();
    }
}

private class firstRunTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Trwa synchronizacja danych");
        mProgressDialog.setMessage("Może to zająć chwilę, proszę czekać.");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        contactService.getAssetsAtFirstRun();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mProgressDialog.dismiss();
        uti.showToast(getBaseContext(), "Zapisano kontakty.");
    }
}

}

Upvotes: 0

Views: 3469

Answers (4)

T_V
T_V

Reputation: 17580

Do this-

private class firstRunTask extends AsyncTask<Void, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog=ProgressDialog.show(MainActivity.this, "Trwa synchronizacja danych", "Może to zająć chwilę, proszę czekać.");

}

@Override
protected Void doInBackground(Void... params) {
    contactService.getAssetsAtFirstRun();
    return null;
}

@Override
protected void onPostExecute(Void result) {

   if(mProgressDialog != null)
{
if(mProgressDialog.isShowing())
  {

       mProgressDialog.dismiss();
        uti.showToast(getBaseContext(), "Zapisano kontakty.");}

    }
 }

Upvotes: 1

Suji
Suji

Reputation: 6044

You are starting an activity after starting the asyctask by calling doFirstRun(); , and thus you are not seeing the progressdialog created. if you remove/comment the startActivity portion as follows, it should work:


doFirstRun();
// comment the following
//Intent i = new Intent(getBaseContext(), ContactListActivity.class);
//startActivity(i);

If you still want to start that activity anyway, then you should start the asynctask after that.

Upvotes: 1

baloo
baloo

Reputation: 217

Try like this 

 private class LongOperation extends AsyncTask<String, Void, String> {

         @Override
         protected String doInBackground(String... params) {


            // Do something

            return "Executed";

         }      

         @Override
         protected void onPostExecute(String result) {
           if(mProgressDialog.isShowing()){
             mProgressDialog.dismiss();
         }


         }

         @Override
         protected void onPreExecute() {
         ShowLoading();
         }

         @Override
         protected void onProgressUpdate(Void... values) {

         }

     }
     private void ShowLoading(){
            mProgressDialog = new ProgressDialog(this);
            //mProgressDialog.setMessage("Loading Please wait ....");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }

Upvotes: 2

Armaan Stranger
Armaan Stranger

Reputation: 3140

create one constructor like:

Context _context;
public firstRunTask(Context context)
{
    _context=context;
}

and use this _context for context in dialog.

Upvotes: 2

Related Questions