0

I have a progress dialog I am trying to show when a user clicks a button to launch a new activity. The spinner should be displayed on the current page until the other activity appears. ( The activity can take sometimes up to 4-5 seconds to launch and without the spinner it just shows a pressed button that looks frozen )

This is what I have, it's only working if I remove hideProgressDialog();, but then the spinner will still be there when I back to the previous activity, obviously.

What am I doing wrong ?

Progress Dialog :

    public void showProgressDialog(Context context) {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
    this.progressDialog = ProgressDialog.show(context, "", "Chargement en cours, veuillez patienter");
}    

public void hideProgressDialog() {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
}

Function :

public void startActivity(Context context, Class<? extends Activity> activityClass) {
    try {
        showProgressDialog(context);
            Intent intent = new Intent(this, activityClass);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         
            startActivity(intent);    
            hideProgressDialog();
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}  

Example of a button click where this calls the function to show the spinner :

    @Override
public void onClick(View view) {
    if(view.getId() == R.id.changeBannerButton) {
        getBaseApplication().startActivity(this, BannerListActivity.class);
    }...
JFFF
  • 991
  • 4
  • 17
  • 31

3 Answers3

6

Call hideProgressDialog() in the onResume() method. This way, if the user presses the back button, the onResume() method gets called and immediately hides the progress dialog.

orange01
  • 1,584
  • 1
  • 16
  • 28
harshk
  • 76
  • 2
2

Well First off you should use the new DialogFragment class with FragmentManager. Because showdialog() is deprecated from API level 8

Next you should use showdialog and removedialog for adding and removing the dialog.

And you should use the onCreateDialog to handle the dialog and the operations. Like start a new thread to run do the job when you are displaying the progressdialog.

Pavan K
  • 4,085
  • 8
  • 41
  • 72
0

Try to load data in a seperate thread on start of activity. But before start of that process show the progress dialog. Now once the process is done use runOnUI to hide the progress dialog..

Indicate the user that data is being loading

http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

and this:

Android - using runOnUiThread to do UI changes from a thread

Community
  • 1
  • 1
Awais Tariq
  • 7,724
  • 5
  • 31
  • 54