Implementation of logout in Android application

Every app will have a "logout" function. When you click to exit, you need to finish all the activities. At first, I wanted to clear all the activities in the stack, but I couldn't find a way. Later, I used broadcasting to achieve it.

main code

 

  • Click to log out: send broadcast
    Intent intent = new Intent();
    intent.setAction("exit_app");
    sendBroadcast (intent);

 

  • Receive broadcast: finish interface (received in BaseActivity)
    MyReceiver receiver;
    private void registerBroadcast() {
        // register broadcast receiver
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("exit_app");
        context.registerReceiver(receiver,filter);
    }
    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(ConstantUtils.EXIT_APP)){
                LogUtils.e("zs","Logout");
                finish();
            }
        }
    }

 

Guess you like

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