Chandra Sekhar
Chandra Sekhar

Reputation: 19500

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

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?

Upvotes: 6

Views: 11922

Answers (2)

AndroDev
AndroDev

Reputation: 3304

Try this:

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

Upvotes: 13

jeet
jeet

Reputation: 29199

use following code to launch home screen:

Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Upvotes: 3

Related Questions