我想在单反向和双反向退出应用程序时返回主页活动,但它不能正常工作,我在下面添加了代码,请帮助我。
if (doubleBackToExitPressedOnce)
{
replaceFragment(new HomeActivity());
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
请帮帮我。
只需将其添加到处理程序运行方法中即可
@Override
public void run() {
replaceFragment(new HomeActivity());
ActivityName.super.onBackPressed();
doubleBackToExitPressedOnce = false;
}
或像这种一样制造其他方法
else{
replaceFragment(new HomeActivity());
super.onBackPressed();
}
private static final int BACK_PRESS = 76;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (BACK_PRESS == msg.what) {
goBack();
}
}
};
private void goBack() {
super.onBackPressed();
}
@Override
public void onBackPressed() {
if (mHandler.hasMessages(BACK_PRESS)) {
mHandler.removeMessages(BACK_PRESS);
replaceFragment(new HomeActivity());
} else {
mHandler.sendEmptyMessageDelayed(BACK_PRESS, 500); // Your activity will close after 500ms
}
}
对于您的案例,您可以使用不同的处理程序。