简体   繁体   English

需要从Android中的应用退出的代码

[英]Need code for exit from app in android

Hello i am new to android.I am implementing some application and it have some activities. 您好,我是android的新手。我正在实现一些应用程序,并且其中包含一些活动。 Suppose if i launch the app for first time,it's entering in to A then going to B after that C,D,E..... (Here A,B,C,D,E are activities).If i press back button at E then it is going D--> C--> B--> A like this. 假设如果我是第一次启动该应用程序,那么它将进入A,然后在C,D,E .....之后进入B(这里是A,B,C,D,E是活动)。如果我按回去在E处按一下按钮,然后按D-> C-> B->A。

Now i want to implement code to exit/quit from the app when i am at D. I wrote following code but this code is working for closing current activity and going to prev activity.means going C. 现在我想实现在D时从应用程序退出/退出的代码。我编写了以下代码,但该代码可用于关闭当前活动并进行prev activity。意味着进入C。

finish(); 完();

Then i tried with following code and it is working fine and closing current application successfully and going to device home screen.But if i want open the application again then it is starting form D instead of A. 然后我尝试使用以下代码,它可以正常工作并成功关闭当前应用程序并进入设备主屏幕。但是如果我想再次打开该应用程序,则它将启动表格D而不是A.

  intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 

copied from here 这里复制

Please help me to solve my problem. 请帮助我解决我的问题。

Use these flags to lunch the activity and clear the activity stack 使用这些标志来午餐活动并清除活动堆栈

Intent intent = new Intent(this, YourActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Be aware that FLAG_ACTIVITY_CLEAR_TASK is only available from API 11 See: http://developer.android.com/guide/components/tasks-and-back-stack.html 请注意,仅API 11才提供FLAG_ACTIVITY_CLEAR_TASK,请参阅: http : //developer.android.com/guide/components/tasks-and-back-stack.html

Try this: 尝试这个:

finish();
System.exit(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM