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

前端 未结 8 1574
一生所求
一生所求 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 14:52

    That will not work. Your activity could get an onPause for reasons other than your app explicitly starting another activity. For instance, the user could have hit the back button, taking you back to a previous Activity in your stack.

    0 讨论(0)
  • 2020-12-09 15:01
        public boolean isCurrentTaskInBackground() {
        List<RunningTaskInfo> 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题