【发布时间】:2015-06-07 19:36:55
【问题描述】:
一般情况下,当点击返回按钮时,我调用finish(),然后我被带回到之前的activity(这是我的MenuActivity):
@Override
public void onBackPressed() {
finish();
}
但是,有时当单击后退按钮并且应用程序简单地退出时,没有其他 activities 运行。有没有办法可以检查activity 是否存在或是否在后台运行/在onPause 状态等...
我想写这样的东西:
@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()
}
}
【问题讨论】:
-
检查this link可能对你有帮助
标签: android android-intent android-activity stack