-1

Is there a way that I can detect the active activity from my BroadcastReceiver?

Here's my code:

class Receiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
         // check the active activity
         // if MainActivity then ....
         // if OtherActivity then ...
   }
}
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • 1
    Possible duplicate of [Android: How can I get the current foreground activity (from a service)?](https://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service) – LarsH Mar 27 '18 at 17:54
  • Anything special you want with this? You can send a broadcast to the system, and have code in every Activity that can hear it and process the message. For those you can actually use LocalBroadcastManager – Marcos Vasconcelos Mar 20 '21 at 01:54

1 Answers1

-1

You can keep a reference on the Application to the last Activity that passed onResume (and clear it when it passes onStop)

In theory, you could get the Activity visible if that variable is not null. In practice if you have transparent Activities some things may not work properly.

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • Can you provide Sallie coffee pls, I tried making reference for the activity in the reviewer, but couldn't call it from the shuttle itself. Thanks – Hasan A Yousef Mar 29 '18 at 11:10