判断某个activity是否存在栈堆中

2019-01-21  本文已影响0人  CQ_TYL
private boolean isExistMainActivity(Class<?> activity) {
        Intent intent = new Intent(this, activity);
        ComponentName cmpName = intent.resolveActivity(getPackageManager());
        boolean flag = false;
        if (cmpName != null) {// 说明系统中存在这个activity    
            ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10);//获取从栈顶开始往下查找的10个activity  
            for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                if (taskInfo.baseActivity.equals(cmpName)) {// 说明它已经启动了    
                    flag = true;
                    break;//跳出循环,优化效率  
                }
            }
        }
        return flag;//true 存在 falese 不存在
    }
上一篇 下一篇

猜你喜欢

热点阅读