Android alertdialog on exit on back press | Exit/No Menu

Android confirmation dialog on back press YES NO to exit application

Android alertdialog on exit show Yes No to confirm exit your application when back button is pressed.

 

package write your package name here;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage(“Do you want to Exit?”);
builder.setPositiveButton(“Yes”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});builder.setNeutralButton(“RateMe”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“market://details?id=com.cubeactive.qnotelistfree”));
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(“https://play.google.com/store/apps/details?id=com.cubeactive.qnotelistfree”)));
}
}
});builder.setNegativeButton(“No”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}

Android press back button again to exit application

if you this above code do not use it back press will be conflict. In this example code it needs twice back click to exit android application. If user pressed back mistakenly, to avoid such accidental back press implement this small code snippet in your application. If user click back button falsely show a toast message, say click back again to exit. Or click back press twice quickly to exit. Android alertdialog on exit Main Activity source code. More new android latest code.

package write your package name here;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static long press_back;
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

@Override
public void onBackPressed() {
if (press_back + 1500 > System.currentTimeMillis()) super.onBackPressed();
else Toast.makeText(getBaseContext(), “Press back again to exit”, Toast.LENGTH_SHORT).show();
press_back
= System.currentTimeMillis();
}

}

Add a Comment

Your email address will not be published. Required fields are marked *