Going to home screen programmatically

AndroidHomescreen

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

One line solution

moveTaskToBack(true); //activity.moveTaskToBack(true);

it will behave as Home Button is pressed

Solution 3 - Android

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);

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSri SriView Question on Stackoverflow
Solution 1 - AndroidJanuszView Answer on Stackoverflow
Solution 2 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 3 - AndroidPeter AjtaiView Answer on Stackoverflow
Solution 4 - Androidst0leView Answer on Stackoverflow
Solution 5 - AndroidTom TaylorView Answer on Stackoverflow
Solution 6 - AndroidJimView Answer on Stackoverflow