Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Activity Using BroadcastReceiver

i have one activity Main.java is open in my application, i want to close the activity using broadcast receiver , how to close the activity?

like image 353
appukrb Avatar asked Sep 10 '26 22:09

appukrb


1 Answers

Firstly your Main.java needs to be registered as a receiver. You could register it in Main.java's onResume():

@Override
public void onResume() {
    registerReceiver(broadcastReceiver, new IntentFilter(BroadcasterClassName.NAME_OF_ACTION));
}

Then handle the broadcast and finish your activity:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BroadcasterClassName.NAME_OF_ACTION)) {
            finish();
        }
    }
}
like image 150
jesobremonte Avatar answered Sep 12 '26 10:09

jesobremonte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!