Going to home screen programmatically

Sri Sri picture Sri Sri · Sep 16, 2010 · Viewed 80.5k times · Source

I want to go to the home screen programmatically in Android when the user clicks on button. How can this be done?

Answer

Janusz picture Janusz · Sep 16, 2010

You can do this through an Intent.

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

This Intent will start the launcher application that the user has defined. Be careful with this because this will look like your application crashed if the user does not expect this.

If you want this to build an exit button from your app please read this article on exit Buttons in Android