Get current visible activity for user

I want to start the foreground work. And I can get this information using the activity manager using the following code.

activityManager = (ActivityManager) FWCommon.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); runningTaskInfoList = activityManager.getRunningTasks(1); componentName = runningTaskInfoList.get(0).topActivity; 

Suppose I have two actions A and B, and I call this code from onResume () above for both actions A and B.

Below is the problem

  • When action A starts with the code above, I'm assigned topActivity = A
  • Then I go from actions A to B and the above code gives me topActivity = B
  • Then I press "Back" to return from action "B" to "A" and "above" the code again gives me topActivity = B instead of A.

thanks Dalvin

+4
source share
1 answer

Try using getRunningAppProcesses () to get a list of RunningAppProcessInfo. Then go through each RunningAppProcessInfo and check if it is in the foreground by doing this:

 List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo process : processes) { // Take a look at the IMPORTANCE_VISIBLE property as well in the link provided at the bottom if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { // Put code here } } 

Click here and here for more information.

+9
source

All Articles