Ask the user if he really wants to quit

Asked

Viewed 758 times

1

I have the following code:

   public boolean onNavigationItemSelected(MenuItem item) {
     // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

        builder = new AlertDialog.Builder(atividade);
        builder.setTitle("Deseja realmente sair?");
        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                finish();
            }
        });
        //Toast.makeText(this, "sair",Toast.LENGTH_SHORT).show();
        finish();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

In the excerpt: I’d like to ask if you’d really like to leave.

If he says yes the application is finished. And in case he says no the screen is kept.

How do you make it work, but do not want!

  • There wouldn’t have to be two buttons for him to choose, and you treat the user’s response?

  • Yes...but I did one didn’t work so it won’t work with 2...

2 answers

2


Try it this way:

  if (id == R.id.nav_send) {

        builder = new AlertDialog.Builder(atividade);
        builder.setTitle("Deseja realmente sair?");
        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                //finish();
               // Estamos dentro do Contexto do botão, 
              // então Usamos o nome da classse + this + método que queremos
              NomeDaActivity.this.finish();
            }
        });
        //Toast.makeText(this, "sair",Toast.LENGTH_SHORT).show();
      //  finish(); -> aqui ele está fechando sempre
     builder.show(); // exibe a dialog 
    }
  • I missed showing off anyway...

1

You can rewrite the method onBackPressed and show a Toast warning if the user double-click back the APP will close. To do so add this code to the end of your activiry } long private backPressedTime = 0; // used by onBackPressed onBackPressed()

@Override
public void onBackPressed() {        // para previnir saídas inesperadas irritantes
    long t = System.currentTimeMillis();
    if (t - backPressedTime > 2000) {    // 2 segundos para sair
        backPressedTime = t;
        Toast.makeText(this, "Seu texto aqui",
                       Toast.LENGTH_SHORT).show();
    } else {    // se pressionado novamente encerrar app
        // clean up
        super.onBackPressed();       // bye
    }

}

To add a specific text in the message that is in the string use Toast.makeText(this.R.string. myText, Toast.LENGTH_SHORT)show();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.