S
S
sitev_ru2015-08-26 06:47:48
Android
sitev_ru, 2015-08-26 06:47:48

How to call the main activity from AlertDialog?

My collapsed application calls AlertDialog. It has two buttons OK and Cancel. With Cancel, we simply close the AlertDialog, but with the OK button, you need to display the main application activity or simply bring the application to the foreground. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bolshakov, 2015-08-26
@enq3

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setTitle("Cообщение")
                .setMessage("Приложение свернули")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(MainActivity.this, MainActivity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.show();

I know that the ApplicationContext is not used to create a dialog, but in the current task context (show the dialog when the application is minimized), the dialog is not displayed on my version of android 4.x, the blank screen is simple, but in version 5 everything is fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question