Android 按下后退按钮时如何退出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2354336/
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
How to exit when back button is pressed?
提问by d-man
When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
当用户按下一个意图上的后退按钮时,应用程序应该退出。如何确保在按下后退按钮时退出应用程序?
回答by Hugo Matilla
In my Home Activity I override the "onBackPressed" to:
在我的家庭活动中,我将“onBackPressed”覆盖为:
@Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
so if the user is in the home activity and press back, he goes to the home screen.
因此,如果用户在主页活动中并按回,他将转到主屏幕。
I took the code from Going to home screen Programmatically
我以编程方式从Going to home screen获取代码
回答by dxh
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
在您开始一项新活动后,立即使用startActivity
,确保您调用finish()
以使当前活动不会堆叠在新活动之后。
EDITWith regards to your comment:
编辑关于您的评论:
What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you cando if you really want to, is to make sure that every startActivity
leading up to that activity, is a startActivityForResult
and has an onActivityResult
listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult
before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in thatactivity, and finish that one, etc...
您的建议并不是特别说明 android 应用程序流程通常如何工作,以及用户期望它如何工作。如果你真的想要,你可以做的是确保每个startActivity
导致该活动的都是一个startActivityForResult
并且有一个onActivityResult
侦听器来检查退出代码,然后冒泡返回。您可以在此处阅读更多相关信息。基本上,setResult
在完成活动之前使用,设置您选择的退出代码,如果您的父活动收到该退出代码,则将其设置在该活动中,然后完成该活动,等等...
回答by Vlad Spreys
A better user experience:
更好的用户体验:
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
@Override
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
回答by Sagar Devanga
The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true"to all the activities that you dont want to be back stacked.And then to close the app call the finish()in the OnBackPressed
仅当返回堆栈中没有活动时,应用才会退出。因此,在您的清单android:noHistory="true" 中将此行添加 到您不想被反向堆叠的所有活动中。然后关闭应用程序,在 OnBackPressed 中调用finish()
<activity android:name=".activities.DemoActivity"
android:screenOrientation="portrait"
**android:noHistory="true"**
/>
回答by Eric
Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.
为什么用户不直接点击主页按钮?然后他们可以从您的任何活动中退出您的应用程序,而不仅仅是特定的活动。
If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).
如果您担心您的应用程序继续在后台执行某些操作。确保在相关的 onPause 和 onStop 命令中停止它(当用户按下 Home 键时会被触发)。
If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.
如果您的问题是您希望下次用户单击您的应用程序时从头开始,我建议在屏幕上放置某种菜单项或 UI 按钮,让用户返回到您的应用程序的起始活动应用程序。像官方推特应用中的推特小鸟等。
回答by Akila
Use onBackPressed
method
使用onBackPressed
方法
@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
This will solve your issue.
这将解决您的问题。
回答by anthonymonori
First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.
首先,Android 不建议您在后退按钮中执行此操作,而是使用提供的生命周期方法。后退按钮不应破坏活动。
Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Activity 被添加到堆栈中,当在 UI 堆栈中最后一个剩余的 Activity 上按下后退按钮时,可以从概述(方形按钮,因为它们在 5.0 中引入了材料设计)访问。如果用户想要关闭您的应用程序,他们应该从“概览”菜单中将其关闭(关闭)。
Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
您的应用负责在 onPause()、onStop() 和 onDestroy() 生命周期方法上停止您不想运行的任何后台任务和作业。请在此处阅读有关生命周期及其正确实施的更多信息:http: //developer.android.com/training/basics/activity-lifecycle/stopping.html
But to answer your question, you can do hacksto implement the exact behaviour you want, but as I said, it is not recommended:
但是要回答您的问题,您可以通过hacks来实现您想要的确切行为,但正如我所说,不建议这样做:
@Override
public void onBackPressed() {
// make sure you have this outcommented
// super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
回答by Uday Shah
To exit from an Android app, just simply use. in your Main Activity, or you can use Android manifest file to set
要退出 Android 应用程序,只需使用。在您的主活动中,或者您可以使用 Android 清单文件来设置
android:noHistory="true"
回答by seekingStillness
I modified @Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.
我修改了@Vlad_Spays 答案,以便后退按钮正常运行,除非它是堆栈中的最后一项,然后它会在退出应用程序之前提示用户。
@Override
public void onBackPressed(){
if (isTaskRoot()){
if (backButtonCount >= 1){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}else{
super.onBackPressed();
}
}
回答by Daniel Nyamasyo
you can simply use this
你可以简单地使用这个
startActivity(new Intent(this, Splash.class));
moveTaskToBack(true);
The startActivity(new Intent(this, Splash.class));
is the first class that will be lauched when the application starts
这startActivity(new Intent(this, Splash.class));
是应用程序启动时将启动的第一个类
moveTaskToBack(true);
will minimize your application
moveTaskToBack(true);
将最小化您的应用程序