How to open dialog to know if you want to close the app (android studio)

0

I'm doing a web app, I've been documenting and I can not find an easy and effective way to open a dialogue when the user is about to close the application and ask him if he really wants to close it or not.

I'm using a webview.

Thanks for the help.

    
asked by Blinder JR 11.07.2018 в 13:10
source

1 answer

0

Create the method:

  private void cerrarAplicacion() {
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.alacran)
                    .setTitle("¿Realmente desea cerrar la aplicación?")
                    .setCancelable(false)
                    .setNegativeButton(android.R.string.cancel, null)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {// un listener que al pulsar, cierre la aplicacion
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            android.os.Process.killProcess(android.os.Process.myPid()); //Su funcion es algo similar a lo que se llama cuando se presiona el botón "Forzar Detención" o "Administrar aplicaciones", lo cuál mata la aplicación
                            //finish(); Si solo quiere mandar la aplicación a segundo plano
                        }
                    }).show();
        }

Send it to the event:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
       cerrarAplicacion();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Or from a Button:

tuBoton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          cerrarAplicacion();
            }
        });
    
answered by 11.07.2018 / 18:12
source