23

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?

sjor
  • 1,438
  • 5
  • 17
  • 22

9 Answers9

59

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);
Anand
  • 1,315
  • 1
  • 12
  • 18
11

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);
MM.
  • 4,224
  • 5
  • 37
  • 74
  • 1
    Strange that opens my SMS app. – rekire Jan 05 '13 at 16:24
  • 3
    This will just open up an app (or show a list of apps) that have the "launcher" category, which is most of them. It explains the behavior reported by rekire. – pents90 May 29 '14 at 21:36
6

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi Sir, i need you help, i tried to create a sample screen locker it works fine, my problem is after the screen lock, the it navigates to device default home, when i click in home button it will launched my launcher but in need to show the default home after the unlock, how can i do it. – Aerrow Dec 17 '13 at 07:55
4

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

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

Community
  • 1
  • 1
Bruce
  • 2,357
  • 5
  • 29
  • 50
1

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.

Community
  • 1
  • 1
1

I have attain it using one line

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

It behave like Home Button

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
1

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

theAlse
  • 5,577
  • 11
  • 68
  • 110
0

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);
ShAkKiR
  • 863
  • 9
  • 20