Ask the user if they really want to quit

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 it says yes, the application is terminated.  And if he says no, the screen is kept.

How do I try to work, but do not want to!

    
asked by anonymous 12.12.2016 / 13:47

2 answers

2

Try this:

  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 
    }
    
12.12.2016 / 13:54
1
___ erkimt ___ Ask the user if they really want to quit ______ qstntxt ___

I have the following code:

@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
    }

}

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

If it says yes, the application is terminated.  And if he says no, the screen is kept.

How do I try to work, but do not want to!

    
______ azszpr171028 ___

Try this:

@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
    }

}
    
______ ___ azszpr171146

You can rewrite the onBackPressed method and Mostar toast one warning if the User double-click on the back will close APP. To do this add this code to the end of your activiry }     private long backPressedTime = 0; // used by onBackPressed onBackPressed ()

%pre%

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

    
___
13.12.2016 / 00:18