Good day!

I think every "more or less" Android developer saw this scheme:

alt text

This is a stack of activities. The figure shows what happens when a new activity starts. So, when studying a single Android application, I ran into a problem. The problem is that I need to somehow get the name of the most privileged activity (the one on the top of the stack). But how to do it? You just need to get the name of the activity that is currently on top of all. I will be glad to any help (any way).

Thank)

  • and why this may need? - Gorets

1 answer 1

Link to an example .

Updated:

Added the implementation of the method to get the name of the upper Activity in the specified application:

 public String getTopActivityName(String appPackage) { final ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE); final List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(Integer.MAX_VALUE); for (final ActivityManager.RunningTaskInfo task : tasks) { if (task.topActivity.getPackageName().equals(appPackage)) { return task.topActivity.getClassName(); } } return null; } 

The only argument - appPackage - as not difficult to guess, is the package name of the application (the one in AndroidManifest.xml).

  • Yes, this is a good example if you need to do this from your application, but I have a different situation: the application is running, here is its main activity at the top of the stack. That is what you need to get. - AseN
  • Well, take the desired ActivityManager.RunningTaskInfo from the returned collection. - falstaf
  • Added method implementation. - falstaf