Dylan Holmes Dylan Holmes - 2 months ago 4
Android Question

Android - Dynamicly Get the Current Activity in Foreground

here is the situation:

  1. In a Thread an event is triggered.

  2. The current activity needs to be retrieved.

  3. Then a Dialog Box is created on that activity and shown.

As far as I've searched there is no way to retrieve the current activity in the foreground.

Extra info:
This needs to be able to be handled in multiple activities. So, it can be popped-up in Activity-A or B or C.


I'm not sure if this is what you're looking for, but it seemed pretty straightforward. http://iamvijayakumar.blogspot.com/2011/09/get-current-activity-and-package-name.html

  ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
  List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
  ComponentName componentInfo = taskInfo.get(0).topActivity;
  Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+"   Package Name :  "+componentInfo.getPackageName());

Hope this helps.