Android 完全退出的实例详解
Android完全退出的实例详解
首先,在基类BaseActivity里,注册RxBus监听:
publicclassBaseActivityextendsAppCompatActivity{ SubscriptionmSubscription; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); Utils.intiSySBar(this,R.color.colorblack); initRxBus(); } //接收退出的指令,关闭所有activity privatevoidinitRxBus(){ mSubscription=RxBus.getInstance().toObserverable(NormalEvent.class) .subscribe(newAction1(){ @Override publicvoidcall(NormalEventuserEvent){ if(userEvent.getType()==-1){ finish(); } } }, newAction1 (){ @Override publicvoidcall(Throwablethrowable){ } }); } @Override protectedvoidonDestroy(){ super.onDestroy(); if(!mSubscription.isUnsubscribed()){ mSubscription.unsubscribe(); } } }
这是事件实体NormalEvent:
publicclassNormalEvent{ privateinttype; publicNormalEvent(inttype){ this.type=type; } publicintgetType(){ returntype; } publicvoidsetType(inttype){ this.type=type; } }
最后,在需要退出的地方调用:
RxBus.getInstance().post(newNormalEvent(-1));//发送退出指令
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!