This is achieved by blocking and rewriting the operation of the back key: when the back key is pressed, a prompt box will pop up to choose whether to exit, if so, execute the exit code, otherwise do nothing.
Override in Mianactivity:
@Override public void onBackPressed() { new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher)//Here is the picture information to display the prompt box, the icon of the default androidApp I use here .setTitle("Confirmation dialog") .setMessage("Do you really want to quit?") .setNegativeButton("Cancel",null) .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).show(); }
Add this code to an android program to get the back button at the end as shown in the figure: