0

I have a method that scans the position and gets called on every activity I have, everytime an actions trigger I need to show an alertDialog, this is the code:

AlertDialog.Builder builder = new AlertDialog.Builder(mMainActivity.this);
                        builder.setMessage("test")
                           .setCancelable(false)
                           .setPositiveButton("go", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                   Intent intent = new Intent(mMainActivity.this,LocationDetails.class);
                                    intent.putExtra("placeId",1);
                                    startActivity(intent);

                               }
                           })
                           .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {

                                    dialog.cancel();
                               }
                           });
                        AlertDialog alert = builder.create();
                        alert.show();

But How can I create the alert on the current activity? Because now it only gets created on AlertDialog.Builder(mMainActivity.this);

LS_
  • 6,763
  • 9
  • 52
  • 88

3 Answers3

1

Have this AlertDialog logic in a public class and make call using a method with passing context .

For example:

showAlert(youractivitycontext);

Use the Context as your current activity.

Gowtham Kumar
  • 534
  • 8
  • 22
1

You have various options... what you have to know:

You can show AlertDialogs from Activity only

You have various options

  • Pass your activity instance as parameter in constructor of other classes... then you can call activity from everywhere

  • Use notifications (class does not need to be an Activity)

  • Check this question. otherwise

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0
create class vis static method in vich you transfer "link" yours activitis 
for example from MainActivity call :
DialogUtils.createSomeDialog(MainActivity.this); 
or from SecondActivity:
DialogUtils.createSomeDialog(SecondActivity.this);


    public class DialogUtils
{
    public static void createSomeDialog(final Context context){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("test")
                .setCancelable(false)
                .setPositiveButton("go", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(context,LocationDetails.class);
                        intent.putExtra("placeId",1);
                        context.startActivity(intent);

                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}
Iatsenko Anton
  • 157
  • 1
  • 6