-1

I would like to make an app which displays some data from the server. When I log in as an admin, I would like there to be a progress dialog until the application gets all the data from the server.

I have 3 Classes. Main Activity(login screen), SecondActivity(displays data) and BackgroundWorker(which extends AsyncTask).

I know that in on postExecute I have to close ProgressBar

Override
protected void onPreExecute() {
        if(activity.getClass() == MainActivity.class) {
            this.progressDialog.setMessage("Please wait for a while.");
            this.progressDialog.setTitle("Login");
            this.progressDialog.show();
        }



        else
            super.onPreExecute();




}

@Override
protected void onPostExecute(final String result) {
    if(activity.getClass() == MainActivity.class) {
        new CountDownTimer(1000, 500) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {
                System.out.println(result);
                    if (result.equals("Username or password is not correct")) {
                    alertDialog.setMessage(result);
                    alertDialog.show();
                } else if(result.equals("is Admin")) {
                        Intent intent = new Intent(activity,Admin.class);
                        intent.putExtra("username",user);
                        activity.startActivity(intent);
                        activity.finish();

                }
                progressDialog.dismiss();
            }
        }.start();
}

I have made like this for login Screen but I don't think it is wise to delay the application on purpose. And also my implementation doesn't work if I call AsyncTask class twice in one activity. Any suggestion?

Swanson
  • 103
  • 1
  • 1
  • 6
  • Possible duplicate of [progressDialog in AsyncTask](https://stackoverflow.com/questions/4538338/progressdialog-in-asynctask) – Pronoy999 Sep 20 '17 at 17:03

1 Answers1

2

You can use this code:

class MyTask extends AsyncTask<Void, Void, Void> {
 ProgressDialog pd;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
       pd = new ProgressDialog(MainActivity.this);
       pd.setMessage("loading");
       pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Do your request
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (pd != null)
      {
         pd.dismiss();
      }
    }
  }

Take a look at this link, if you want!

Good luck with your android development!

Pronoy999
  • 645
  • 6
  • 25