1

How to open new activity on progressdialog dimiss?

Here is my part of code:

            @Override
        public void onClick(View v) {
            progressDoalog = new ProgressDialog(Hack.this);
            progressDoalog.setMax(100);
            progressDoalog.setMessage("Its loading....");
            progressDoalog.setTitle("ProgressDialog bar example");
            progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDoalog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (progressDoalog.getProgress() <= progressDoalog
                                .getMax()) {
                            Thread.sleep(200);
                            handle.sendMessage(handle.obtainMessage());
                            if (progressDoalog.getProgress() == progressDoalog
                                    .getMax()) {
                                progressDoalog.dismiss();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        Handler handle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                progressDoalog.incrementProgressBy(1);
            }
        };
    });
}

Please help me guys. I think that this is the one solution to open activity after progressdialog complete.

  • Possible duplicate of [Can you fire an event when Android Dialog is dismissed?](https://stackoverflow.com/questions/6203720/can-you-fire-an-event-when-android-dialog-is-dismissed) – EKN May 29 '17 at 10:14

3 Answers3

1

Just add :

progressDoalog.setOnDismissListener(new DialogInterface.OnDismissListener(){
            @Override
            public void onDismiss(DialogInterface dialog) {
                //Launch your activity
            }
        });

It's a simple Listener which is called when your dialog dismiss

Firerazzer
  • 192
  • 1
  • 13
0

You can use an AsyncTask

There are 3 important methods to override in AsyncTask.

  1. doInBackground : this is where the meat of your background processing will occur.
  2. onPreExecute : show your ProgressDialog here ( showDialog )
  3. onPostExecute : hide your ProgressDialog here ( removeDialog or dismissDialog )

If you make your AsyncTask subclass as an inner class of your activity, then you can call the framework methods showDialog, dismissDialog, and removeDialog from within your AsyncActivity.

Here's a sample implementation of AsyncTask:

class ProgressTask extends AsyncTask<String, Integer, Boolean> {
  @Override
  protected Boolean doInBackground(String... params) {
    try {
      Thread.sleep(4000);  // Do your real work here
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return Boolean.TRUE;   // Return your real result here
  }
  @Override
  protected void onPreExecute() {
    showDialog(AUTHORIZING_DIALOG);
  }
  @Override
  protected void onPostExecute(Boolean result) {
    // result is the value returned from doInBackground
    removeDialog(AUTHORIZING_DIALOG);
    Intent i = new Intent(this, LandingActivity.class);
    startActivity(i);
  }
}
vm345
  • 813
  • 12
  • 28
0

@Markus Maleńki Please refer below code

if(progressDoalog!=null && progressDoalog.isShowing()){
  progressDoalog.setOnDismissListener(new 
  DialogInterface.OnDismissListener(){
  @Override
    public void onDismiss(DialogInterface dialog) {
    //Launch your activity
   }
});

}

EKN
  • 1,886
  • 1
  • 16
  • 29