Sei sulla pagina 1di 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

49

Going to home screen programmatically


android

homescreen

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

share

improve this question


Sri Sri
1,085 5 18 33

Asked
Sep 16 '10 at 7:22

Jonik
34.5k 40 170 231

11

Edited
Dec 20 '13 at 18:59

please, accept some of your older questions. Yossarian Sep 16 '10 at 7:25
A simple way is to override onBackPressed or through keyEvent =Backpressed and call onHomePressed on it. then it will behave like homePressed Nepster May 29 '14 at 13:27
add a comment

4 Answers

94

Order By

Votes

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

share

improve this answer


Janusz
77.5k 85 247 319

Answered
Sep 16 '10 at 8:41

Edited
Oct 15 '14 at 7:25

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? Enigma Nov 19 '12 at 7:05

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

@portfoliobuilder, you just need to remove FLAG_ACTIVITY_NEW_TASK. user802421 Oct 30 '15 at 6:46

what does ACTION_MAIN and CATAGORY_HOME refer to? Srujan Barai Jan 1 at 11:50
add a comment

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

share

improve this answer


Peter Ajtai
36.6k 9 90 118
Janusz
77.5k 85 247 319

One line solution


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

it will behave as Home Button is pressed

Answered
Mar 15 '12 at 19:50

Edited
Oct 15 '14 at 7:27

share

improve this answer


Nepster
9,763 2 69 81

Answered
Aug 7 '15 at 7:42

It's working.. Is it a good approach.. Will it work in every case? Prabs Nov 20 '15 at 5:52

In my situation it works fine.I am not quiet sure whether it works in all cases or not Nepster Nov 26 '15 at 7:55
add a comment

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.

share

improve this answer


Community Wiki

3 revs, 2 users 92%


st0le
Edited
Sep 16 '10 at 8:55

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. Janusz Sep 16 '10 at 8:37
@Janusz, I didn't mean to sound harsh... st0le Sep 16 '10 at 8:54
add a comment

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Potrebbero piacerti anche