Android: How to detect if current stack of activities (task) moves to background?

前端 未结 8 1607
一生所求
一生所求 2020-12-09 14:24

The official documentation describes tasks as follows:

*All the activities in a task move together as a unit. The entire task (the entire activity sta

8条回答
  •  忘掉有多难
    2020-12-09 15:01

        public boolean isCurrentTaskInBackground() {
        List taskInfoList = mActivityManager.getRunningTasks(1);
        if (taskInfoList != null && taskInfoList.size() > 0) {
            RunningTaskInfo info = taskInfoList.get(0);
    
            ComponentName componentName = info.baseActivity;
    
            MobileTvLog.debug("App#isCurrentTaskInBackground -> baseActivity = " + componentName);
    
            String packageName = componentName.getPackageName();
    
            return !YOUR_PKG_NAME.equals(packageName);
        } else {
            return true;
        }
    }
    

提交回复
热议问题