How to exit the android application on backpress?

前端 未结 4 1345
夕颜
夕颜 2021-01-07 04:21

The situation could be like say, I\'ve 5 activities. Say Main activity, Activity 1, Activity 2, Activity 3 and Activity 4.

One can use the Activity 1,2,3 & 4 dir

相关标签:
4条回答
  • 2021-01-07 04:53

    You need to Override onBackPressed() inside your MainActivity() and use finishAffinity to finish all activities

    onBackPressed()

    Called when the activity has detected the user's press of the back key.

    finishAffinity() : added in API level 16

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

    SAMPLE CODE

    @Override
    public void onBackPressed() {
      super.onBackPressed();
      ActivityCompat.finishAffinity(MainActivity.this);
    }
    

    EDIT

    I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.

    @Override
    public void onBackPressed() {
    
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Alert")
                    .setCancelable(false)
                    .setMessage("Are your sure want to exit?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.finishAffinity(MainActivity.this);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
    
                        }
                    });
    
            builder.show();
    
    }
    
    0 讨论(0)
  • 2021-01-07 04:55

    this is commonly used way to exit the app, also i recommend using "nohistory" attribute in manifest for activities other than MainActivity.

    boolean doubleBackToExitPressedOnce = false;
    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }
    
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    
    new Handler().postDelayed(new Runnable() {
    
        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;                       
        }
    }, 2000);
    

    }

    and for other activities use nohistory method in manifest

         <activity
            android:name="com.omniapay.salesservice.Activities.LoginActivity"
            android:label="LOGIN"
            android:noHistory="true" />
    
    0 讨论(0)
  • 2021-01-07 04:58

    add your MainActivity

    @Override
    public void onBackPressed() {
    finish();
    super.onBackPressed();
    }
    
    0 讨论(0)
  • 2021-01-07 05:02

    It is a good code practice to write double back press to exit Main Activity.

    boolean doubleBackToExitPressedOnce = false;
    
    @Override
    public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    
    new Handler().postDelayed(new Runnable() {
    
        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;                       
        }
    }, 2000);
    

    }

    0 讨论(0)
提交回复
热议问题