KCRaju
KCRaju

Reputation: 544

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.

Upvotes: 2

Views: 8473

Answers (2)

lukasz
lukasz

Reputation: 3131

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

Upvotes: 4

tsuensiu
tsuensiu

Reputation: 186

Try this:

finish();
System.exit(0);

Upvotes: 0

Related Questions