2

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.

Community
  • 1
  • 1
KCRaju
  • 544
  • 3
  • 7
  • 21
  • Have a look at: http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically and http://stackoverflow.com/questions/2092951/how-to-close-android-application – Seraphim's May 30 '13 at 15:46
  • what you need is a action bar. On click on application icon navigate to app main screen. Click back button to finish the activity. http://developer.android.com/guide/topics/ui/actionbar.html – Raghunandan May 30 '13 at 15:51
  • @Raghunandan :: Sorry i am not getting what you are telling. can you elaborate please. – KCRaju May 30 '13 at 15:56
  • check this http://developer.android.com/design/patterns/navigation.html. Use action bar. http://www.vogella.com/articles/AndroidActionBar/article.html – Raghunandan May 30 '13 at 15:57
  • @Raghunandan :: I need code to quit my app properly not for BACK button – KCRaju May 30 '13 at 16:00
  • @user2431524 that's why i said use a action bar. On click of app icon navigate to home screen of your activity and then click back button to exit from your app. – Raghunandan May 30 '13 at 16:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30927/discussion-between-raghunandan-and-user2431524) – Raghunandan May 30 '13 at 17:13
  • My exact req was i need to close all activities and goto device home screen or device applications screen when i click the "Exit" button from my app and when i ever relaunch the application it should start from MAIN activity – KCRaju May 30 '13 at 17:14
  • @user2431524 so what's wrong in using actionbar for that puprpose. Did you check this http://developer.android.com/design/patterns/navigation.html. check the gmail app on your phone. Clearly the links shows how to code of navigation using Action bar – Raghunandan May 30 '13 at 17:17
  • sorry, i am new to android and i don't know exactly what actionbar will do and how to implement my requirement by using actionbar :(.If you have any info please help me. – KCRaju May 30 '13 at 17:20
  • @user2431524 why don't you check the comments and the links posted? http://www.vogella.com/articles/AndroidActionBar/article.html. posted the same earlier also – Raghunandan May 30 '13 at 17:22
  • can we quit/exit from app by using actionbar? – KCRaju May 30 '13 at 17:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30959/discussion-between-raghunandan-and-kcraju) – Raghunandan May 31 '13 at 05:53

2 Answers2

4

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

lukasz
  • 3,121
  • 1
  • 21
  • 20
  • What will happen if the user wants to navigate back to previous activity say from activity D to E? – Raghunandan May 30 '13 at 16:00
  • His problem is not about the back button but about the activity stack. If his app has no purpose to go back from activity D to activity C (activity C displays a login form for example), then clearing the activity stack makes sense here. – lukasz May 30 '13 at 16:05
  • clearing the back stack will defeat the purpose as users can't navigate to previous activity. I would still suggest the OP to use a action bar. – Raghunandan May 30 '13 at 16:09
  • check the link here http://developer.android.com/design/patterns/navigation.html. Shows navigation with snap shots – Raghunandan May 30 '13 at 16:11
  • Most of the time I would agree with you. But in some specific cases where the user must not go back to the previous activity (login or any other one time use activity), clearing the activity stack is a proper way to go – lukasz May 30 '13 at 16:13
  • where does the op mention he has a login activity ( may be i missed to see that part of the qustion)in his question?. – Raghunandan May 30 '13 at 16:16
  • My exact req was i need to close all activities and goto device home screen or device applications screen when i click the "Exit" button from my app and when i ever relaunch the application it should start from MAIN activity. – KCRaju May 30 '13 at 17:17
0

Try this:

finish();
System.exit(0);
tsuensiu
  • 186
  • 1
  • 9
  • don't use system.exit(0).http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan May 30 '13 at 15:54
  • My exact req was i need to close all activities and goto device home screen or device applications screen when i click the "Exit" button from my app and when i ever relaunch the application it should start from MAIN activity. – KCRaju May 30 '13 at 17:17