Going to home screen programmatically
Asked Answered
A

6

80

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

Appendage answered 16/9, 2010 at 7:22 Comment(1)
A simple way is to override onBackPressed or through keyEvent =Backpressed and call onHomePressed on it. then it will behave like homePressedView
B
186

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

Better answered 16/9, 2010 at 8:41 Comment(10)
I'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?Amaro
Simplest 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.)Paraboloid
This is an older thread, but although this works returning back to the previous Activity does not work. What attributes to that?Cytochemistry
@portfoliobuilder, you just need to remove FLAG_ACTIVITY_NEW_TASK.Americano
what does ACTION_MAIN and CATAGORY_HOME refer to?Futch
this saved me a lot of troubleSixtieth
@Janusz, when using the above code in OnBackPressed(), getting the crash with java.lang.SecurityException: Permission Denial: starting IntentPlumlee
I'm seeing many crashes on firebase with a similar approach, posted it here and don't have a solution yet: #55006298Plaided
this closes my applicationHalibut
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.Quire
V
33

One line solution

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

it will behave as Home Button is pressed

View answered 7/8, 2015 at 7:42 Comment(5)
It's working.. Is it a good approach.. Will it work in every case?Dermatome
In my situation it works fine.I am not quiet sure whether it works in all cases or notView
Works well for me (Android 6.x)Pegg
Works well for me too (android 7.x)Gromme
this closes my application (Android 9.x)Halibut
M
11

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);
Mapping answered 15/3, 2012 at 19:50 Comment(0)
O
1

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.
Orate answered 16/9, 2010 at 7:22 Comment(1)
stOle 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.Better
P
1
startActivity((new Intent(Intent.ACTION_MAIN)).addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Pepsinogen answered 12/7, 2019 at 4:42 Comment(0)
I
0

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.

Interfertile answered 6/9, 2017 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.