Question: I need to close the current activity from a broadcast receiver. I'm not sure how to call finish from it, maybe there is a way to simulate a "Back" key keypress. Any implementation will be fine as long as it does the job.

```
@Override
public void onReceive(Context context, Intent intent) {
// How can I finish the current activity here?
}

```

Answer: 
The [ActivityManager class](http://developer.android.com/reference/android/app/ActivityManager.html) can give you the current foreground activity (even if it's not from your app). The [getRunningTasks](http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29) methods will give you a list of the running tasks, the first element of the list being the most recent launched activity.Unfortunately,this method will just give you an object of type [RecentTaskInfo](http://developer.android.com/reference/android/app/ActivityManager.RecentTaskInfo.html) , not the activity itself, so there is no way to call its finish() method,I believe :/ 

On the other hand, if you want to close the current activity from your app, you can implement a static variable on a personal class that each activiy would set in their onResume() method. This way you will always know what activity is the current one. But I guess it's not what you are looking for.

Edit: The getRunningTasks is just intended for debug purposes, as says the doc..
Place `finish();` after u completed all the tasks in `onReceive()` of `BroadcastReceiver` class as below: 

```
 @Override
 public void onReceive(Context context, Intent intent) {
    // Do all the tasks onReceive of BroadCast Receiver
    finish(); // This finishes the current activity here....   
}

```