Displaying a progress dialog until new activity loads

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);
    }...

Answers


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.


Need Your Help

primefaces <p:select> cant get values

java jsf jpa primefaces

When i push this buttom , the test() mothod cant print any value . And the System.out.println(person); always print null in handleCityChange() , and frame displays red and the upper right corner to...

How to find latitude and longitude in node.js?

node.js express latitude-longitude

I am working on web analytics, we are using node.js in server side, we are sending the data using http request(express framework).

jQuery.get() works on IE, but jQuery.post() does not

jquery ajax internet-explorer

I have a very strange problem. I'm making a pretty standard ajax-call with jQuery, but it refuses to work with Internet Explorer 7.