一般來說,當點擊後退按鈕時,我打電話finish()
,和我帶回以前activity
(這是我MenuActivity):如何檢查現有活動時,後退按鈕按下
@Override
public void onBackPressed() {
finish();
}
然而,有時候沒有其他的activities
在後退按鈕被點擊並且應用程序退出時運行。有沒有一種方法,我可以檢查是否有activity
存在,或者在後臺運行/在狀態等等
我還想寫是這樣的:如果你知道
@Override
public void onBackPressed() {
if(isActivitiesInStack) //if there are still activities in stack
finish(); //simply call finish and be taken back to next activity
else { // else, there are no acitivites in the stack
Intent intent = new Intent(ThisActivity.this, MenuActivity.class); // then create an intent and send me to the menu acitivy
startActivity(intent);
finish()
}
}
檢查[此鏈接(不建議使用HTTP://計算器.com/a/6242122/2715073)可能會對您有所幫助 – Pankaj