我有一个包含登录活动的应用程序,当应用程序启动时会显示给用户,问题是应用程序关闭但没有按回按钮上的OnDestroy()按
代码: -
private long lastPressTime = 1;
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if ((currentTime - lastPressTime) < 2000) {
System.exit(0);
} else {
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
lastPressTime = currentTime;
}
}
答案 0 :(得分:2)
使用finish()
代替System.exit(0)
,
@Override
public void onBackPressed() {
finish();
}
希望这会对你有所帮助。
答案 1 :(得分:0)
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if ((currentTime - lastPressTime) < 2000) {
System.exit(0);//
onDestroy(); // but you can use finish(); here Destroy will be called
} else {
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
lastPressTime = currentTime;
}
}