首先,在基類BaseActivity里,注冊(cè)RxBus監(jiān)聽(tīng):
public class BaseActivity extends AppCompatActivity { Subscription mSubscription; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Utils.intiSySBar(this, R.color.colorblack); initRxBus(); } //接收退出的指令,關(guān)閉所有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(); } }}這是事件實(shí)體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; }}最后,在需要退出的地方調(diào)用:
RxBus.getInstance().post(new NormalEvent(-1));//發(fā)送退出指令
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注