Android 完全退出的实例详解
程序员文章站
2023-12-13 22:19:16
android 完全退出的实例详解
首先,在基类baseactivity里,注册rxbus监听:
public class baseactivity exten...
android 完全退出的实例详解
首先,在基类baseactivity里,注册rxbus监听:
public class baseactivity extends appcompatactivity { subscription msubscription; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); utils.intisysbar(this, r.color.colorblack); initrxbus(); } //接收退出的指令,关闭所有activity private void initrxbus() { msubscription = rxbus.getinstance().toobserverable(normalevent.class) .subscribe(new action1<normalevent>() { @override public void call(normalevent userevent) { if (userevent.gettype() == -1) { finish(); } } }, new action1<throwable>() { @override public void call(throwable throwable) { } }); } @override protected void ondestroy() { super.ondestroy(); if (!msubscription.isunsubscribed()) { msubscription.unsubscribe(); } } }
这是事件实体normalevent:
public class normalevent { private int type; public normalevent(int type) { this.type = type; } public int gettype() { return type; } public void settype(int type) { this.type = type; } }
最后,在需要退出的地方调用:
rxbus.getinstance().post(new normalevent(-1));//发送退出指令
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!