Exit android app on back pressed

后端 未结 12 1930
逝去的感伤
逝去的感伤 2021-01-30 10:57

I am building an Android App. How to exit an Android App when back is pressed. Android version is 2.3.3 and above. The Android App goes to previous activity which i don\'t want.

相关标签:
12条回答
  • 2021-01-30 11:44

    moveTaskToBack(); is now deprecated... Instead use : moveTaskToBack(true);

    0 讨论(0)
  • 2021-01-30 11:46

    You can clear all the back stack doing this.

    @Override
    public void onBackPressed() {
        finishAffinity();
    }
    
    0 讨论(0)
  • 2021-01-30 11:52

    In normal way you shouldn't close app - user will moves it to back by pressing home or closing your activities by pressing back.

    But you can use this tips:

    1) Close old activities after starting the new ones if they not needed in back stack:

    startActivity(newActivityIntent);
    finish();
    

    2) You can move task to back (instead of close) if you need http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

    3) If you are really need it, you can use System.exit(0) but it's strongly not recommended and usually says that you have application's architecture problems.

    0 讨论(0)
  • 2021-01-30 11:52

    Just use the code finish(); under your intent to close the activity. So, It will not appear again. This solved mine.

    startActivity(new Intent(SplashActivity.this, MainActivity.class));
    finish();
    
    0 讨论(0)
  • 2021-01-30 11:54

    Try this

    public void onBackPressed(){
    Intent a = new Intent(Intent.ACTION_MAIN);
    a.addCategory(Intent.CATEGORY_HOME);
    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(a);
    
    }
    
    0 讨论(0)
  • 2021-01-30 11:58

    Pop Up

         @Override
         public void onBackPressed() {
         new AlertDialog.Builder(this)
           .setTitle("Really Exit?")
           .setMessage("Are you sure you want to exit?")
           .setNegativeButton(android.R.string.no, null)
           .setPositiveButton(android.R.string.yes, new OnClickListener() {
    
        public void onClick(DialogInterface arg0, int arg1) {
        WelcomeActivity.super.onBackPressed();
            }
        }).create().show();
    

    }

    Set Class to Top of App and no history

    Intent launchNextActivity;
    launchNextActivity = new Intent(B.class, A.class);
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);                  
    
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(launchNextActivity);
    

    enter code here Double back press

    private static long back_pressed;
    @Override
    public void onBackPressed(){
    if (back_pressed + 2000 > System.currentTimeMillis()){
    super.onBackPressed();
    }
    else{
    Toast.makeText(getBaseContext(), "Press once again to exit", 
    Toast.LENGTH_SHORT).show();
    back_pressed = System.currentTimeMillis();
    }
    

    }

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