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);
    }

}

```
Not sure about whether this is helpfull to you or not but its help me once and i think thats a same case here so i am answering for you.

Whenever the broadcast receiver get call, you can navigate to any activity by clicking on that broadcast message.

Just like:

```
@Override
public void onReceive(Context context, Intent intent) {
    // My Notification Code
    notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.app_icon;
    //System.out.println("The ID Number is: "+Long.parseLong(intent.getData().getSchemeSpecificPart()) );
    contentText = intent.getStringExtra("MyMessage");
    System.out.println("The Message is: "+intent.getStringExtra("MyMessage"));
    CharSequence text = "Your tax amount due period";
    CharSequence contentTitle = "Tax Toolbox";

    long when = System.currentTimeMillis();

    intent = new Intent(context, MenuPageActivity.class); // here i am calling activity
    intent.putExtra("sixMonth", "sixMonth");
    intent.putExtra("messageSixMonth", contentText);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification = new Notification(icon,text,when);

    long[] vibrate = {0,100,200,300};
    notification.vibrate = vibrate;  // To vibrate the Device

    notification.ledARGB = Color.RED;
    notification.ledOffMS = 300;
    notification.ledOnMS = 300;

    notification.defaults |= Notification.DEFAULT_LIGHTS;
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notificationManager.notify(com.project.TaxToolbox.NotificationConstants.NOTIFICATION_ID_SIX_MONTH, notification);

}

```

Now, on the onCreate() of that activity you have to identify whether it is call by Notification or not. 

As like:

```
  NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    System.out.println("The Extra for twoMonth is:  "+getIntent().hasExtra("twoMonth"));
    System.out.println("The Extra for sixMonth is:  "+getIntent().hasExtra("sixMonth"));
    System.out.println("The Extra for EveryMonth is:  "+getIntent().hasExtra("everyMonth"));

    if(getIntent().hasExtra("sixMonth")){
        notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_SIX_MONTH);
        final AlertDialog alert3 = new AlertDialog.Builder(MenuPageActivity.this).create();
        alert3.setTitle("Tax Toolbox");
        alert3.setMessage(getIntent().getExtras().getString("messageSixMonth"));
        alert3.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        alert3.setIcon(R.drawable.app_icon);
        alert3.show();

```

// here you can do anything more or close the activity.
 }

Not sure but might be helpfull to you.

Feel free to comments if it help you.