Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android singleton dialog

I have android application which deals with lot of progress dialogs.I have to create a separate dialog for each activity.

Dialog creation takes a activity(context) as parameter while constructing.

Is there a way by which I can create a single dialog (which is tied to application and not the activity) and show it in different activity so that I do not have to create it repeatedly.

like image 358
Rahul Avatar asked Dec 13 '22 17:12

Rahul


1 Answers

Declare showProgressDialog and hideProgressDialog in Utill helper class as shown in following code snippet

public static ProgressDialog showProgressDialog(Context context) {
        ProgressDialog pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        return pDialog;
    }

    public static void hideProgressDialog(ProgressDialog pDialog) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

Then call from activity where you need to show the ProgressDialog for example in onPreExecute() method of AsyncTask class as shown in below code snippet

ProgressDialog pDialog = Util.showProgressDialog(this);

and use following code to hide the progressDialog

 Util.hideProgressDialog(pDialog);
like image 140
Amandeep Rohila Avatar answered Jan 17 '23 12:01

Amandeep Rohila