Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Dynamically 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.

Problems: 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.

like image 432
Dylan Holmes Avatar asked Aug 07 '13 23:08

Dylan Holmes


2 Answers

Plan A: taking your request literally

Step #1: Have the thread be managed by a service

Step #2: have the service send a message when the "even is triggered" -- LocalBroadcastManager, Square's Otto, greenrobot's EventBus, etc.

Step #3: Have each activity be set up to listen for that message when it is in the foreground

Step #4: Have the activity display the dialog upon receipt of the message


Plan B: same visual result

Step #1: Have the thread be managed by a service

Step #2: Have the service call startActivity() on a dialog-themed activity

Step #3: There is no step #3

like image 121
CommonsWare Avatar answered Nov 10 '22 11:11

CommonsWare


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.

like image 40
Prmths Avatar answered Nov 10 '22 12:11

Prmths