按下后退出Android应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21253303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:13:11  来源:igfitidea点击:

Exit android app on back pressed

android

提问by Buru

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.

我正在构建一个 Android 应用程序。按下后退键时如何退出 Android 应用程序。安卓版本为2.3.3及以上。Android 应用程序转到我不想要的先前活动。

回答by A.Wie

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);

}

回答by Buru

Some Activities actually you don't want to open again when back button pressed such Splash Screen Activity, Welcome Screen Activity, Confirmation Windows. Actually you don't need this in activity stack. you can do this using=> open manifest.xml file and add a attribute

有些活动实际上您不想在按下后退按钮时再次打开,例如启动屏幕活动、欢迎屏幕活动、确认窗口。实际上,您不需要在活动堆栈中使用它。您可以使用=> 打开 manifest.xml 文件并添加一个属性

android:noHistory="true"

机器人:noHistory =“真”

to these activities.

到这些活动。

<activity
    android:name="com.example.shoppingapp.AddNewItems"
    android:label="" 
    android:noHistory="true">
</activity>

OR

或者

Sometimes you want close the entire application in certain back button press. Here best practice is open up the home window instead of exiting application. For that you need to override onBackPressed() method. usually this method open up the top activity in the stack.

有时您希望按某些后退按钮关闭整个应用程序。这里的最佳做法是打开主窗口而不是退出应用程序。为此,您需要覆盖 onBackPressed() 方法。通常这种方法会打开堆栈中的顶部活动。

@override
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}

OR

或者

in back button pressed you want to exit that activity and also you also don't want to add this in activity stack. call finish() method inside onBackPressed() method. it will not make close the entire application. it will go for the previous activity in the stack.

在按下后退按钮时,您想退出该活动,而且您也不想将其添加到活动堆栈中。在 onBackPressed() 方法中调用 finish() 方法。它不会关闭整个应用程序。它将用于堆栈中的上一个活动。

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

onBackPressed - 当活动检测到用户按下后退键时调用

public void onBackPressed() {
  finish();
}

回答by Tara

I know its too late, but no one mentioned the code which i used, so It will help others.

我知道为时已晚,但没有人提到我使用的代码,所以它会帮助其他人。

this moveTaskToBackwork as same as Home Button. It leaves the Back stack as it is.

这项moveTaskToBack工作与主页按钮相同。它保持原样离开 Back 堆栈。

 public void onBackPressed() {
 //  super.onBackPressed();
    moveTaskToBack();
  }

回答by André Luiz Reis

You can clear all the back stack doing this.

您可以清除所有后台堆栈执行此操作。

@Override
public void onBackPressed() {
    finishAffinity();
}

回答by Sujith Royal

I found an interesting solution which might help.

我找到了一个有趣的解决方案,可能会有所帮助。

I implemented this in my onBackPressed()

我在我的 onBackPressed() 中实现了这个

finishAffinity();
finish();

FinishAffinity removes the connection of the existing activity to its stack. And then finish helps you exit that activity. Which will eventually exit the application.

FinishAffinity 删除现有活动与其堆栈的连接。然后完成帮助您退出该活动。这将最终退出应用程序。

回答by Duan Bressan

If you have more than one screen in history and need all other ones to be ignored. I recommend using this:

如果历史记录中有多个屏幕并且需要忽略所有其他屏幕。我建议使用这个:

@Override
public void onBackPressed() {

    finish();

}

回答by Dimmerg

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

在正常情况下,您不应该关闭应用程序 - 用户将通过按 home 将其移回或通过按回关闭您的活动。

But you can use this tips:

但是您可以使用以下提示:

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

1)如果在后堆栈中不需要旧活动,则在启动新活动后关闭旧活动:

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)

2)如果您需要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 recommendedand usually says that you have application's architecture problems.

3)如果你真的需要它,你可以使用它,System.exit(0)强烈不推荐它,通常说你有应用程序的架构问题。

回答by Prajwal W

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

moveTaskToBack(); 现在已弃用...改为使用: moveTaskToBack(true);

回答by Waqas Shah

Kotlin:

科特林:

 override fun onBackPressed() {
        val exitIntent = Intent(Intent.ACTION_MAIN)
        exitIntent.addCategory(Intent.CATEGORY_HOME)
        exitIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivity(exitIntent)
    }

Other codes have problems, some does not work and some needs API classification.

其他代码有问题,有的不行,有的需要API分类。

回答by DEEP ADHIYA

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

将 Class 设置为 Top of App 并且没有历史记录

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();
}

}

}