我关注了this tutorial和某些类似的答案。
我目前的onBackPressed
代码如下 -
private static final int TIME_DELAY = 2000;
private static long back_pressed;
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
int fragments = getFragmentManager().getBackStackEntryCount();
if (fragments > 0) {
super.onBackPressed();
} else {
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(getBaseContext(), "Press once again to exit!",
Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
}
}
}
我正在向这样的堆栈添加片段(在某些地方我不会添加到后台) -
private void LoadSignDetailsFragment() {
Bundle args = new Bundle();
Fragment fragment = new SignDetailsFragment();
args.putBoolean("hasValues", true);
args.putBoolean("showBookmarkedSignsOnly", showBookmarkedSignsOnly);
args.putInt("sign_id", signId);
if (fragment != null) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
我要做的是,如果backstack中有任何片段,则单个onBackPressed
会迁移到之前的片段。但是,如果在Backstack中没有Fragment,它应该显示Toast for double back press以关闭应用程序。
我现在的代码,总是显示Toast,并要求Double back press,而不管Backstack中是否存在碎片。我无法弄明白为什么?
答案 0 :(得分:25)
您可以根据需要参考以下代码。如果您不使用v4支持片段,则必须使用getFragmentManager()
而不是getSupportFragmentManager()
来获取backstack计数。这里我使用布尔值来检查是否单击了后退,如果在2秒内没有再次单击它,它将再次变为假。
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
//Checking for fragment count on backstack
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else if (!doubleBackToExitPressedOnce) {
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);
} else {
super.onBackPressed();
return;
}
}
答案 1 :(得分:1)
private boolean doubleBackToExitPressedOnce = true;
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = false;
Toast.makeText(this,"Please click BACK again to exit.", Toast.LENGTH_SHORT).show();
} else {
finish();
}
}
}
答案 2 :(得分:0)
以简单的方式做到这一点 使用下面的代码
Boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
System.exit(0);
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit",
Toast.LENGTH_SHORT).show();
}
答案 3 :(得分:0)
在片段onCreateView
中添加此代码 view.setOnKeyListener( new OnKeyListener()
{
private Boolean exit = false;
@Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
//logic for identifying double back press, expires after 3 seconds
if (exit) {
getActivity().finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
return true;
}
return false;
}
} );
或者如果你想在你的活动中添加它,你必须覆盖onBackPressed methhod并添加相同的代码。
例如
private Boolean exit = false;
@Override
public void onBackPressed(){
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
但建议在片段
以外的活动中进行答案 4 :(得分:0)
没有处理程序的方式:
@Override
public void onBackPressed() {
long currentMillis = System.currentTimeMillis();
if (currentMillis - this.lastPressed < 2000 && getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
// show toast if you need
}
this.lastPressed = currentMillis;
}