0

When I press the log out button and come to start(main) screen and when I press the back button from main screen it comes to 2nd screen. but, i want to close the application on click of back button from main screen and close all the activities before that. while doing research I found,

Intent intent = new Intent(this, Home.class);  
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
finish();

but its not satisfying my requirement.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Java_Android
  • 735
  • 2
  • 9
  • 15

2 Answers2

3

You can use this code that I referred from here How to exit from the application and show the home screen?:

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

Although Android's design does not favor exiting an application by choice.

Related Links:

How to close Android application?

Android exit application

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK worked for me – KOTIOS Jul 22 '13 at 06:41
  • @RachitaNanda: Hi, i have another doubt.. your above code is satisfying my requirement. but when i press mobile's back button (not my app's back button) it comes to 2nd screen.. it should close the app. and one more thing that is when i press back button from the app(i did the above code on back button) and when i start the app again its not displaying splash screen.. but directly giving login screen.?? tell me the solution. – Java_Android Jul 24 '13 at 07:42
  • mobile's back button finishes the current activity and takes you to previous activity on stack .If you want same behavior on hard key back press ,just override onbackpressed() or onkeydown() of activity and add your code there.I can't say anything about the other problem without the code ,you can ask another question on SO as its a different prob – Rachita Nanda Jul 24 '13 at 07:53
0

just add line in your code and this will work fine

   startActivity(intent);
Rishabh Bhardwaj
  • 832
  • 8
  • 20