I want to go to the home screen programmatically in Android when the user clicks on button. How can this be done?
-
A simple way is to override onBackPressed or through keyEvent =Backpressed and call onHomePressed on it. then it will behave like homePressed – Zar E Ahmer May 29 '14 at 13:27
6 Answers
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

- 187,060
- 113
- 301
- 369
-
3I've read that this is very wrong to do in an App. But why do people say it? I know where I'm using it. And it looks like that's the only thing I can do to do what I want to do. So is this ok to use it? – Sudarshan Bhat Nov 19 '12 at 07:05
-
6Simplest thing that works for me: `startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));` (FLAG_ACTIVITY_NEW_TASK doesn't seem to be necessary.) – Jonik Dec 20 '13 at 18:58
-
This is an older thread, but although this works returning back to the previous Activity does not work. What attributes to that? – portfoliobuilder Sep 25 '14 at 18:49
-
2
-
-
-
@Janusz, when using the above code in OnBackPressed(), getting the crash with java.lang.SecurityException: Permission Denial: starting Intent – mahesh Feb 01 '19 at 09:30
-
I'm seeing many crashes on firebase with a similar approach, posted it here and don't have a solution yet: https://stackoverflow.com/questions/55005798/illegalstateexception-cant-change-activity-type-once-set – David Apr 09 '19 at 11:27
-
-
If your app was not started from the home screen (e. g. from the apps screen), you need Intent.FLAG_ACTIVITY_CLEAR_TASK as well. – Richard Parkins Jun 22 '22 at 06:11
One line solution
moveTaskToBack(true); //activity.moveTaskToBack(true);
it will behave as Home Button is pressed

- 33,936
- 20
- 234
- 300
-
-
In my situation it works fine.I am not quiet sure whether it works in all cases or not – Zar E Ahmer Nov 26 '15 at 07:55
-
1
-
1
-
Janusz's answer is great.
The only thing I want to add, which is a little too long for a comment, is that you can go to the home screen without having a reference to the current activity.
Janusz's code needs to be called from an Activity or Fragment due to startActivity()
,
To get around this, you can store a static reference to your apps Context in your application file:
public class YourApplication extends Application
{
private static Context mAppContext;
public void onCreate()
{
super.onCreate();
...
YourApplication.mAppContext = getApplicationContext();
}
public static Context getContext()
{
return mAppContext;
}
}
Now you can send the user to the device home screen from any class in your app, not just Activities, Fragments, or other Classes with a reference to the current Activity (you can decide whether this is a good or bad thing):
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
YourApplication.getContext().startActivity(startMain);

- 1
- 1

- 56,972
- 13
- 121
- 140
From Android developer site
Here are some examples of other operations you can specify as intents using these additional parameters:
* ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.

- 33,375
- 8
- 89
- 89
-
3stOle there is nothing wrong with asking questions that can be answered with a quick google query and a look into the documentation. If the question contains the correct keywords it will show up on the first google page very quickly and makes finding the correct information and sample code much much faster then to look into the documentation. I'm a somehow trained android programmer but I would have to google and to skip over one or two pages to answer this question. It would be great if the answer to this question is here where it can be edited, improved, updated and rated. – Janusz Sep 16 '10 at 08:37
startActivity((new Intent(Intent.ACTION_MAIN)).addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

- 3,344
- 2
- 38
- 63
I know this is a bit late but I also ran into the same problem and here is how I resolved it. Going back to your MainActivity
you need to add flags from the exiting Activity
final Intent mainActivity = new Intent(this, MainActivity.class);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Now when you pressed the back button being MainActivity
the active one, it will go to home screen.

- 1,027
- 1
- 10
- 19