6

In my app, I have a button called EXIT, when the user clicks that, I want to finish all the activities of my app, which are in the stack, and go to the default home activity or the all apps activity.

I have written the following code in my onClick():

Intent intent = new Intent(Intent.CATEGORY_HOME);
startActivity(intent); 

But it gives me the following error in logcat:

03-12 11:22:18.279: ERROR/AndroidRuntime(308): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.category.HOME }

So what do I need to do to achieve this? Do I need some configuration in the manifest or is my approach wrong?

Daniel
  • 2,355
  • 9
  • 23
  • 30
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 1
    possible duplicate of [Going to home screen programmatically](http://stackoverflow.com/questions/3724509/going-to-home-screen-programmatically) – Jim G. Mar 01 '14 at 16:45

2 Answers2

13

Try this:

Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • 1
    Is it finishing all the activities from the stack task? – Chandra Sekhar Mar 12 '12 at 06:14
  • 1
    No. This code will take you to the home screen. I just gave you the code to to go to the default home screen. (As I see in your log, it was throwing exception). To finish all your activities of your application, you will have to write logic which should get called before this code. – AndroDev Mar 12 '12 at 06:17
  • will Intent.FLAg_ACTIVITY_CLEAR_TOP do that, as every activity is on the top of the home activity. – Chandra Sekhar Mar 12 '12 at 06:21
  • 1
    I guess finish() method may be helpful – Android Mar 12 '12 at 06:32
  • As per doc of FLAG_ACTIVITY_CLEAR_TOP = "For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B." For that you should start your application from home app only. I also tried above code with FLAG_ACTIVITY_CLEAR_TOP, it retains the state and does not clear the back stack. You can simply check this by calling above code, then just long press the home button. Your app is running with old state. – AndroDev Mar 12 '12 at 06:40
  • Chandra is actually correct. If you have a launcher, and you programmatically go to HOME as you have answered, all the activities are finished from the stack task. Try it, you will see that you will not be returned to the Activity you launched the default Home screen from – portfoliobuilder Sep 25 '14 at 18:51
  • Causes ActivityNotFoundException for some users of my app having devices with Android 5.1.1. Details: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] } – Ercument Kisa Nov 16 '15 at 15:46
3

use following code to launch home screen:

Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
jeet
  • 29,001
  • 6
  • 52
  • 53