java – How to Launch Home Screen Programmatically in Android-ThrowExceptions

Exception or error:

I want to launch home screen of Android with my application. The main target is to show all of apps to user when he/she presses a specialized key. Actually, the way is not important. Any idea to do this?

How to solve:

Here is the code for starting HomeActivity

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

###

The comments you made on some of the answers suggest you actually want to launch the Launcher (you may want to update the title if this is the case). To do this, use the same approach Anand proposed for launching the home activity.

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

###

There is no “screen that shows apps with their icons to users” in Android.

What you are thinking of is a feature of some home screens. There is no standardized Intent to trigger this to appear, and there is no requirement for home screens to have such a feature.

You are welcome to write your own. Here is a sample project that displays launchable activities in a ListView.

###

try something like this to click back button any you will goto home screen/…

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Display confirmation here, finish() activity.
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

###

I am not sure if I completely understand what you are trying to do!
But if you mean that you want the user to be able to another application by clicking inside your application, then you should check out “intent”.
Run the API DEMO sample code in eclipse, and run App -> Intents

###

I think I am very late to the party, but I had a similar concern. The answers given here launch a selection menu which allows you to choose the Launcher. If you have more than one launcher in your code, the answer here: https://stackoverflow.com/a/8666155 might be of help. This directly launches the default home screen of Android.

###

None of the solutions here is working for me..

I got it working by using the below code

PackageManager pm = getPackageManager();
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.HOME");
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
if (lst != null) {
   for (ResolveInfo resolveInfo : lst) {
       try {
       Intent home = new Intent("android.intent.action.MAIN");
       home.addCategory("android.intent.category.HOME");
       home.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
       startActivity(home);
       break;
       } catch (Throwable t) {
           t.printStackTrace();
       }
   }
}

Got it from: https://stackoverflow.com/a/16483596/1241783

Hope this helps someone

###

I have attain it using one line

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

It behave like Home Button

###

This is working good for me!

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

can somebody explain why we need this ?

startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

btw, this is what I was looking for

moveTaskToBack(true);

Leave a Reply

Your email address will not be published. Required fields are marked *