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: 
At your broadcast receiver write:
`YourCurrentActivityName.this.finish();`

Or you can terminate the front activity with this.finish(); so the last open in stuck comes to front.

---

**Update:** 
Code for first case:

Use of broadcast receiver to terminate activity at back stack:

```
public class ActivityFirstName extends Activity {

    private BroadcastReceiver mFinishReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // other code

        if (mFinishReceiver == null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("com.example.ACTION_TERMINATE");// a string to identify your action
            mFinishReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // How can I finish the current activity here?
                    if ("com.example.ACTION_TERMINATE".equals(intent.getAction())) {
                        ActivityFirstName.this.finish();
                    }
                }
            };
            registerReceiver(mFinishReceiver, intentFilter);
        }

        // other code

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isFinishing()) {
            if (mFinishReceiver != null) {
                unregisterReceiver(mFinishReceiver);
            }
        }
    }

}

```

And the front/current running activity, the sender of the broadcast:

```
public class ActivitySecondName extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        // code code code

        final Button button = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Perform action on click
                terminateBackActivities();
            }
        });
    }

    private void terminateBackActivities() {
        Intent i = new Intent("com.example.ACTION_TERMINATE"); // the two action strings MUST be same
        // i.putExtra(...); // to send extra data
        sendBroadcast(i);
    }

}

```
Assuming from your comment that the BroadcastReceiver is not an internal class of the activity, here is what you should do: Rather than having the broadcast receiver in a separate class, define it inside your activity like so:

```
private BroadcastReceiver mFinishReceiver = new BroadcastReceiver(){
@Override
    public void onReceive(Context context, Intent intent){
        YourActivity.this.finish();
    }
};

```

Then, you will want to register the receiver in onResume() as such:

```
@Override
public void onResume(){
    super.onResume();
    registerReceiver(mFinishReceiver, new IntentFilter(yourIntentAction));
}

```

You will also want to unregister this receiver in onPause() so you don't leak it:

```
@Override
public void onPause(){
    super.onPause();
    unregisterReceiver(mFinishReceiver);
}

```

Then you can remove the other receiver that had its own separate class and also remove its definition in the manifest. The above example will ensure that you can always call finish() with no issues because the receiver is only registered when the activity is running, as it is internal to the activity's class.

EDIT: Change the methods to onCreate() and onDestroy() rather than onPause() and onDestroy(), according to madlymad's comment.