Android Activity Home Get goHome2(Activity ctx)

Here you can find the source of goHome2(Activity ctx)

Description

go Home

License

Apache License

Declaration

public final static void goHome2(Activity ctx) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Main {
    public final static void goHome2(Activity ctx) {
        PackageManager packageManager = ctx.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfos = packageManager
                .queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
        String myPackageName = ctx.getPackageName();
        for (ResolveInfo ri : resolveInfos) {
            if (myPackageName.equals(ri.activityInfo.packageName)) {
                continue;
            }/*from ww  w .  j  a v a  2  s .co  m*/
            intent = new Intent();
            ComponentName name = new ComponentName(
                    ri.activityInfo.packageName, ri.activityInfo.name);
            intent.setComponent(name);
            // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            // Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ctx.startActivity(intent);
            ctx.moveTaskToBack(true);
            ctx.finish();
            ctx.overridePendingTransition(0, 0);
            break;
        }
    }
}

Related

  1. goHome(Activity ctx)
  2. getHostActivity()