How to go to the default home screen of Android programatically?

Chandra Sekhar picture Chandra Sekhar · Mar 12, 2012 · Viewed 11.2k times · Source

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?

Answer

AndroDev picture AndroDev · Mar 12, 2012

Try this:

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