今天就跟大家聊聊有關Android中怎么實現完全退出,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
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));//發送退出指令
看完上述內容,你們對Android中怎么實現完全退出有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。