BÀI 41 - TÙY CHỈNH NÚT BACK TRONG LẬP TRÌNH ANDROID

Trong java, dưới hàm Oncreate(). Gọi hàm onBackPress() và code như sau:

@Override

public void onBackPressed() {


AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Nội dung");

alert.setCancelable(false);


alert.setNegativeButton("Tên nút - HỦY", (dialog, which) -> dialog.cancel());


alert.setPositiveButton("Tên nút - OK", (dialog, which) -> {

Intent exitApp = new Intent(Intent.ACTION_MAIN);

exitApp.addCategory(Intent.CATEGORY_HOME);

exitApp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(exitApp);

});

alert.create();

alert.show();

}