判断activity是否在栈中存在

fun isExsitMianActivity(context: Context): Boolean {

val intent = Intent(context, FirstMainActivity::class.java)

val cmpName = intent.resolveActivity(context.packageManager)

var flag =false

    if (cmpName !=null){// 说明系统中存在这个activity

        val am = context.getSystemService(ACTIVITY_SERVICE)as ActivityManager

if (am !=null){

val taskInfoList = am.getRunningTasks(10)

for (taskInfoin taskInfoList) {

if (taskInfo.baseActivity == cmpName) {// 说明它已经启动了

 flag =true

                    break//跳出循环,优化效率

                }}}}

return flag

}

你可能感兴趣的:(判断activity是否在栈中存在)