如何处理片段中的 BackPress?
How to handle BackPress in fragments?
我的应用程序中有多个片段,按 doubleBackPress 退出。问题是,无论 activity.
上显示哪个片段,退出的 doubleBackPress 都会被激活
这是我试过的。
MainActivity中的代码
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (doubleBackToExitPressedOnce) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run(){
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
这就是我启动片段的方式。
fragmentManager = getFragmentManager() ;
fragmentTransaction = fragmentManager.beginTransaction();
CategoryFragment categoryFragment = new CategoryFragment();
fragmentTransaction.replace(R.id.container,categoryFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
您可以根据需要参考以下代码。如果您不使用 v4 支持片段,则必须使用 getFragmentManager()
而不是 getSupportFragmentManager()
来获取后台堆栈计数。这里我是用boolean值来检测back是否被点击,如果2秒后没有再次点击,它会再次变为false。
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;
}
}
希望对您有所帮助。
您正在替换片段试试这个:
fragmentTransaction.add(R.id.container,categoryFragment);
而不是
fragmentTransaction.replace(R.id.container,categoryFragment);
我的应用程序中有多个片段,按 doubleBackPress 退出。问题是,无论 activity.
上显示哪个片段,退出的 doubleBackPress 都会被激活这是我试过的。
MainActivity中的代码
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (doubleBackToExitPressedOnce) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run(){
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
这就是我启动片段的方式。
fragmentManager = getFragmentManager() ;
fragmentTransaction = fragmentManager.beginTransaction();
CategoryFragment categoryFragment = new CategoryFragment();
fragmentTransaction.replace(R.id.container,categoryFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
您可以根据需要参考以下代码。如果您不使用 v4 支持片段,则必须使用 getFragmentManager()
而不是 getSupportFragmentManager()
来获取后台堆栈计数。这里我是用boolean值来检测back是否被点击,如果2秒后没有再次点击,它会再次变为false。
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;
}
}
希望对您有所帮助。
您正在替换片段试试这个:
fragmentTransaction.add(R.id.container,categoryFragment);
而不是
fragmentTransaction.replace(R.id.container,categoryFragment);