How to Launch Home Screen Programmatically in Android [duplicate]

岁酱吖の 提交于 2019-11-27 04:05:14

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

radioactivet

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.

Bruce

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!