這篇文章主要介紹了Android中如何實現(xiàn)完全退出的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Android中如何實現(xiàn)完全退出文章都會有所收獲,下面我們一起來看看吧。
Android 完全退出的實例詳解
首先,在基類BaseActivity里,注冊RxBus監(jiān)聽:
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() { @Override public void call(NormalEvent userEvent) { if (userEvent.getType() == -1) { finish(); } } }, new Action1 () { @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; } }
最后,在需要退出的地方調(diào)用:
RxBus.getInstance().post(new NormalEvent(-1));//發(fā)送退出指令
關(guān)于“Android中如何實現(xiàn)完全退出”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Android中如何實現(xiàn)完全退出”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。