Skip to content Skip to sidebar Skip to footer

Calling An Alert Dialog From Another Class

I am trying to call an alert dialog from another class but this is not letting me set it to static. It shows as only final is permitted and that means it cannot call it from the ot

Solution 1:

Its a better idea to define all your dialogs in a base class , lets call it ... well BaseActivity

Class BaseActivity extendsActivity{

intDIALOG_X=1;
intDIALOG_Y=2;
intDIALOG_Z=3;
// More Dialog identifiers 

ProgressDialog progressDialog;
AlertDialog alertDialog;
//More dialog objects if you needprotected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_X:
        // do the work to define the X Dialogbreak;
    case DIALOG_Y:
        // do the work to define the Y Dialogbreak;
    default:
        dialog = null;
    }
    return dialog;
}
}

Then in another class extend BaseActivity and call

showDialog(DIALOG_X); 

when you need to show Dialog_X

Solution 2:

Create a Constructor, where you can get Activity. Like this -

Activity activity;
publicYourClass(Activity activity){
         this.activity = activity;
}

Now, use this activity as argument -

AlertDialog.Builder adb=new AlertDialog.Builder(activity);

Because dialog can't be shown using just a context. You need to provide an Activity for that.

Solution 3:

You can also simply extend AlertDialog, and make your own and reuse.

Post a Comment for "Calling An Alert Dialog From Another Class"