Use the Android broadcast mechanism to close multiple activities

I encountered a problem during development, and I need to go back to the login interface after changing the password. This requires closing all the previous activities, which is also very simple:
Add the following code to each activity:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                @Override
               public void onReceive(Context context, Intent intent) {
                       unregisterReceiver(this); // This sentence must be written or an error will be reported. Although it can be closed if not written, a bunch of errors will be reported.
                       ((Activity) context).finish();
                    }
            };

                @Override
       public void onResume() {
               super.onResume();

               // Register the broadcast in the current activity
                    IntentFilter filter = new IntentFilter();
               filter.addAction("liu");
               registerReceiver(this.broadcastReceiver, filter); // 注册
            }

    public void close() {
                Intent intent = new Intent();
                intent.setAction("liu"); // Description action
                sendBroadcast(intent);// This function is used to send broadcast
                finish();
    }
You can call the close() method on the button at the trigger time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691884&siteId=291194637