在菜单打开时覆盖 BackButton
Override BackButton while menu is Open
我想要实现的是,当用户在 MENU
可见时单击后退按钮时,menuActual
和 MENU
的状态从可见变为不可见。如果 MENU
未打开并且用户单击返回,则会显示 Toast
并显示 "Press again to Exit"
,如果您在 2 秒内单击返回,应用程序将关闭。
我拥有的代码:
@Override
public void finish() {
if (MENU.getVisibility() == View.VISIBLE){
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);
}else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
moveTaskToBack(true);
return;
}else {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
}`
我已经声明了boolean doubleBackToExitPressedOnce = false;
应用程序会显示 Toast
说 "Press again to Exit"
,但如果再次单击后退,应用程序会显示 "AppName isn't responding"
努力弄清楚这是为什么,这是漫长的一天。
谢谢!
在 onBackPressed
中这样做:
private boolean doubleBackToExitPressedOnce = false;
private Handler handler;
private Runnable runnable;
@Override
public void onBackPressed() {
if (MENU.getVisibility() == View.VISIBLE) {
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);
return;
}
if (!doubleBackToExitPressedOnce) {
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
return;
}
// Removes the callBack
handler.removeCallbacks(runnable);
// Replace this next line with finishAffinity() if you want to close the app.
super.onBackPressed();
}
我想要实现的是,当用户在 MENU
可见时单击后退按钮时,menuActual
和 MENU
的状态从可见变为不可见。如果 MENU
未打开并且用户单击返回,则会显示 Toast
并显示 "Press again to Exit"
,如果您在 2 秒内单击返回,应用程序将关闭。
我拥有的代码:
@Override
public void finish() {
if (MENU.getVisibility() == View.VISIBLE){
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);
}else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
moveTaskToBack(true);
return;
}else {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
}`
我已经声明了boolean doubleBackToExitPressedOnce = false;
应用程序会显示 Toast
说 "Press again to Exit"
,但如果再次单击后退,应用程序会显示 "AppName isn't responding"
努力弄清楚这是为什么,这是漫长的一天。
谢谢!
在 onBackPressed
中这样做:
private boolean doubleBackToExitPressedOnce = false;
private Handler handler;
private Runnable runnable;
@Override
public void onBackPressed() {
if (MENU.getVisibility() == View.VISIBLE) {
MENU.setVisibility(View.INVISIBLE);
menuActual.setVisibility(View.INVISIBLE);
return;
}
if (!doubleBackToExitPressedOnce) {
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap again to Exit!", Toast.LENGTH_SHORT).show();
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
return;
}
// Removes the callBack
handler.removeCallbacks(runnable);
// Replace this next line with finishAffinity() if you want to close the app.
super.onBackPressed();
}