阅读this并在Android文档中搜索后,我放弃了,并决定在这里询问。
我尝试在Navigation Drawer
中设置后退按钮,以便应用仅从Home
片段关闭,其他任何片段都会将我们带回Home
。例如,如果我们从Home
片段开始并导航到导航抽屉中的其他片段,当我们点击后退按钮时,我们将转到Home
片段,如果我们从Home片段回击,我们将退出该应用。
我试图像这样实现它:
public void onNavigationDrawerItemSelected(int position) {
...
// If the next fragment is Home, clear the backstack.
if (frag instanceof Home) {
getSupportFragmentManager().popBackStack();
fragmentManager.beginTransaction().replace(R.id.container, frag)
.commit();
}
// If the backstack already contains a transaction, replace the current fragment
else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
fragmentManager.beginTransaction().replace(R.id.container, frag)
.commit();
}
// If the backstack is empty, insert the Home -> Fragment transaction
else {
fragmentManager.beginTransaction().replace(R.id.container, frag)
.addToBackStack("").commit();
}
...
}
...
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(Gravity.START)) {
drawerLayout.closeDrawer(Gravity.LEFT);
} else {
super.onBackPressed();
// If returning back to Home fragment, restore action bar title.
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
onSectionAttached(0);
}
}
我附加的链接无法正常工作,我实施的方式并不一致。例如,如果我从Home导航到其他片段并返回主页(不按后退按钮),我的片段会相互叠加。
答案 0 :(得分:2)
我刚刚找到了一种让这项工作变得相当简单的方法,所以我决定将它发布在这里:
// Are we in Home fragment?
private boolean homeFrag = true;
...
@Override
public void onNavigationDrawerItemSelected(int position) {
...
// Next fragment is Home?
if (frag instanceof Home)
homeFrag = true;
else
homeFrag = false;
fragmentManager.beginTransaction().replace(R.id.container, frag)
.commit();
onSectionAttached(position);
}
...
@Override
public void onBackPressed() {
// Close navigation drawer if open
if (drawerLayout.isDrawerOpen(Gravity.START)) {
drawerLayout.closeDrawer(Gravity.LEFT);
} else {
// If not in Home, move back to Home.
if (!homeFrag) {
onNavigationDrawerItemSelected(0);
} else
super.onBackPressed();
}
}
我想听听您对此解决方案的看法。
答案 1 :(得分:0)
我认为你在onBackPressed方法中对nav ul ul {
height: auto;
display: none;
position:absolute;
background-color: #399077;
margin-left: 89.5%;
//top: 9%; delete this line
margin-top: -38px; //add this line
z-index:150;
}
的调用应该是else块中的最后一个代码。原因是方法调用可能会影响getBackStackEntryCount()中的计数。我认为这些电话已经太多了。
答案 2 :(得分:0)
我已经制作了这种方法,可以为你管理片段和后台堆栈。
/**
* Change the current displayed fragment managing fragment backstack
*
* @param frag the fragment to display
* @param saveInBackstack true if you want the previous to be in backstack
*/
private void changeFragment(Fragment frag, boolean saveInBackstack) {
String backStateName = ((Object) frag).getClass().getName();
try {
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(backStateName) == null) { //fragment not in back stack, create it.
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.main_fragment_container, frag, ((Object) frag).getClass().getName());
if (saveInBackstack) {
Log.d(TAG, "Change Fragment : addToBackTack");
transaction.addToBackStack(backStateName);
} else {
Log.d(TAG, "Change Fragment : NO addToBackTack");
}
transaction.commit();
} else {
Log.d(TAG, "Change Fragment : nothing to do");
// custom effect if fragment is already instanciated
}
} catch (IllegalStateException exception) {
Log.e(TAG, "Unable to commit fragment, could be activity as been killed in background. " + exception.toString());
}
}
同样在您的活动中,覆盖onBackPressed:
@Override
public void onBackPressed() {
int fragments = getFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
finish();
}
super.onBackPressed();
}