How to close Android application?

后端 未结 22 1197
小蘑菇
小蘑菇 2020-11-22 07:17

I want to close my application, so that it no longer runs in the background.

How to do that? Is this good practice on Android platform?

If I rely on the \"ba

相关标签:
22条回答
  • 2020-11-22 08:13

    Use "this.FinishAndRemoveTask();" - it closes application properly

    0 讨论(0)
  • 2020-11-22 08:15

    I think it will close your activity and all Sub activity related to it.

    public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();]
            if (id == R.id.Exit) {
                this.finishAffinity();
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
  • 2020-11-22 08:16

    Simply write the following code in onBackPressed:

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    
        //Creating an alert dialog to logout
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Do you want to Exit?");
        alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        startActivity(intent);
                    }
                });
    
        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
    
                    }
                });
    
        //Showing the alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
    
    0 讨论(0)
  • 2020-11-22 08:16

    Use of finishAffinity() may be an good option if you want to close all Activity of the app. As per the Android Docs-

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
    
    0 讨论(0)
提交回复
热议问题