How to exit from the application and show the home screen?

后端 未结 20 1998
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:45

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button \"EXIT\" which when clicked should t

相关标签:
20条回答
  • 2020-11-22 10:29
    System.exit(0);
    

    Is probably what you are looking for. It will close the entire application and take you to the home Screen.

    0 讨论(0)
  • 2020-11-22 10:29

    Maybe my code can hepls (Main_Activity.java):

        @Override
        protected void onDestroy() {
            super.onDestroy();
            this.finish();
            exit(0);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)  {
            switch(keyCode)    {
                case KeyEvent.KEYCODE_BACK:
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("My application").setMessage("Keep playing?").setIcon(R.drawable.icon);
                    // Go to backgroung
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { moveTaskToBack(true); }
                    });
                    // Exit from app calling protected void onDestroy()
                    builder.setNegativeButton("CLOSE APPLICATION", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { onDestroy(); }
                    });
                    // Close this dialog
                    builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { dialog.cancel(); }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    return true;
            }
            return false;
        }
    
    0 讨论(0)
提交回复
热议问题