How to exit an Android app programmatically?

前端 未结 29 972
孤城傲影
孤城傲影 2020-11-28 03:54

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this

相关标签:
29条回答
  • 2020-11-28 04:09

    put this one into your onClic:

    moveTaskToBack(true);
        finish()
    
    0 讨论(0)
  • 2020-11-28 04:09

    ghost activity called with singletop and finish() on onCreate should do the trick

    0 讨论(0)
  • 2020-11-28 04:11

    Just call these two functions

     finish();
     moveTaskToBack(true);
    
    0 讨论(0)
  • 2020-11-28 04:11

    If you use both finish and exit your app will close complitely

    finish();

    System.exit(0);

    0 讨论(0)
  • 2020-11-28 04:12
    finish();
     finishAffinity();
     System.exit(0);
    

    worked for me

    0 讨论(0)
  • 2020-11-28 04:13
     @Override
        public void onBackPressed() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("Exit Application?");
            alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    moveTaskToBack(true);
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                    System.exit(1);
                                }
                            })
    
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
    
                            dialog.cancel();
                        }
                    });
    
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    
    0 讨论(0)
提交回复
热议问题