11

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dylan Holmes
  • 335
  • 1
  • 6
  • 17
  • possible duplicate of [How to get any identifier of the topmost activity?](http://stackoverflow.com/questions/3393908/how-to-get-any-identifier-of-the-topmost-activity) – sschuberth Jul 09 '14 at 12:42
  • http://stackoverflow.com/a/28423385/185022 – AZ_ Feb 10 '15 at 03:07
  • Possible duplicate of [How to get current foreground activity context in android?](http://stackoverflow.com/questions/11411395/how-to-get-current-foreground-activity-context-in-android) – Alessandro Muzzi Dec 03 '16 at 13:42

6 Answers6

16

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.

Prmths
  • 2,022
  • 4
  • 21
  • 38
6

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hooray for step 2 (in both plans)! But why is step 1 (using a service) important, vs. using an AsyncTask for example? – LarsH Feb 16 '17 at 16:14
  • 1
    @LarsH: If the background work will go on long enough that you do not know what the foreground UI is (or even if it is in your own app), you probably want a service, just to signal to Android that you are doing that background work, so your process can live a bit longer. – CommonsWare Feb 16 '17 at 16:17
  • Step 2 in Plan B assumes that one knows for which activity ```startActivity()``` is to be called. This is the crux of the problem. I want the Activity on the top of my task to be started. How to get hold of that Activity? – Raghubansh Mani Oct 31 '17 at 10:52
  • 1
    @RaghubanshMani: In your case, you use Plan A. Have your activities register for the event in `onStart()` and unregister in `onStop()`. Whichever activity is presently visible will process the event. – CommonsWare Oct 31 '17 at 10:53
  • Having each Activity register and unregister seems a bit repetitive and wasteful, but, I guess there is no avoiding it here. Putting it into a BaseActivity superclass feels abusing. – Raghubansh Mani Oct 31 '17 at 10:59
  • @RaghubanshMani: Registering with an event bus is fairly cheap, and if you happen to have a `BaseActivity` already, registering in there would seem to be a good plan. I don't know that I would necessarily introduce a common superclass just for two lines of code, but if you already have such a superclass, use it. – CommonsWare Oct 31 '17 at 11:03
  • I do have a ```BaseActivity``` superclass. But I am hesitant to put such code there. My purpose for having such base classes is to provide common helper methods which all the sub-classes will use, like, fragment transaction, resource access, etc. I don't like putting behavioral logic there because I rarely open such files. It could easily become an unwanted feature in a new sub-class. It's just a preference for me - keep behavior code in front of eyes. – Raghubansh Mani Oct 31 '17 at 11:16
0

You can achieve this using a custom broadcast receiver. You can define a broadcast receiver in your thread and write the receiver in different activities. And from your thread you can decide which activity to send broadcast.

In the receiver, you can show the dialog.

Sushil
  • 8,250
  • 3
  • 39
  • 71
0

You may do that by extending Activity and Application and having methods that get and set the current viewed Activity reference as an Application level variable. Try this approach: How to get current foreground activity context in android?.

Community
  • 1
  • 1
frogmanx
  • 2,620
  • 1
  • 18
  • 20
  • Hehe, that approach is completely equivalent to storing current Activity in a static variable. – zapl Aug 08 '13 at 00:57
  • Nope, the top scoring answer doesn't use a static variable – frogmanx Aug 08 '13 at 12:19
  • No but it uses the Application to reference an Activity. Application is never garbage collected and therefore Activity is never garbage collected when referenced from Application. That is equivalent to using static variables since they are referenced from their `Class` object which is as well never garbage collected. – zapl Aug 08 '13 at 12:30
  • Yeah, I wouldn't do it completely the samesince there is a cleaner and memory efficient way to complete the same task. But the variables are nullified onPause so it should be garbage collected still – frogmanx Aug 08 '13 at 12:56
  • If you null them in `onPause` you could as well use a static variable :) If you can't guarantee that this happens you get the leak either way. Only thing you can (and maybe should do anyways) is to keep a `WeakReference` to them. It's guaranteed they they don't get GC'd until after the Activity hits `onDestroy`. – zapl Aug 08 '13 at 13:19
0

Use Face book's stetho package is safest and simplest way for me (and no complaints from other team members that "it's hacky we won't approve!" thing...

Just as simple as:

ActivityTracker.get().tryGetTopActivity();

0

just like @Xian Zhang said, using Facebook stetho lib is working for me

ActivityTracker.get().tryGetTopActivity()?.localClassName

If your activity is within the package ui , the output be like

    ui.YourActivityName

your app package name will not be included

justnoone
  • 111
  • 2
  • 5