简体   繁体   中英

Need code for exit from app in android

Hello i am new to android.I am implementing some application and it have some activities. 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.

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.

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.

  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

Try this:

finish();
System.exit(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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