当我使用FragmentManager中的replace函数时,片段被添加到堆栈顶部而不删除旧版本。我能够在线找到的所有问题都源于人们在FragmentManager版本之间切换时,但正如您所看到的,我只使用最新版本。
case 2:
getGMCompApp().changeActivitiesFragment(new ITServiceCenterListFragment(), true);
break;
-
public void changeActivitiesFragment(Fragment frag, boolean addToBackStack, boolean addIfSameFragment) {
try {
if (frag == null) {
// Incoming fragment is null, cancel operation
return;
}else if(getCurrentActivity() != null){
//Activity is not null, attempt to change the activity
//Grab the fragment manager
FragmentManager fragmentManager = getCurrentActivity().getFragmentManager();
if(shouldIncomingFragmentShow(frag, addIfSameFragment)){
//The fragment should be swapped out
setCurrentFragment(frag);
if(!addToBackStack){
//The previous fragment should NOT be added to backstack navigation
fragmentManager.beginTransaction()
.replace(R.id.container, frag).commit();
Toast.makeText(getApplicationContext(), "Did not add to back", Toast.LENGTH_SHORT).show();
} else {
// Add the current transaction to the backstack for
// easier navigation
int backStackCount = fragmentManager.getBackStackEntryCount();
String tag = Integer.toString(backStackCount);
//Checks to see if fragment already exists somewhere on backStack
for(int i = 0; i < backStackCount; i++){
//Toast.makeText(getApplicationContext(), "Cmp: " + frag.getClass().getSimpleName() +" with " + fragmentManager.getBackStackEntryAt(i).getName(), Toast.LENGTH_SHORT).show();
if(frag.getClass().getSimpleName().equals(fragmentManager.getBackStackEntryAt(i).getName())){
//Fragment duplicateFragment = fragmentManager.findFragmentByTag(Integer.toString(i));
//tag = Integer.toString(i);
//fragmentManager.beginTransaction().remove(duplicateFragment).commit();
}
}
fragmentManager
.beginTransaction()
.replace(R.id.container, frag)
.addToBackStack(frag.getClass().getSimpleName())
.commit();
}
if(frag.getClass().equals(ViewImageFragment.class))
{
getCurrentActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else if(getCurrentActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
getCurrentActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
相关的XML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="2dp"
android:background="#111"/>
我还试图手动删除并添加片段,我无法删除实际删除片段,我认为这与replace相同(删除部分失败但添加成功)。< / p>
感谢。
我创建了自己的解决方案并发布,以防其他人帮助。我手动管理片段并在数组中创建自己的堆栈。它将所有片段存储在新版本和旧版本的重复片段之间,然后弹出复制品。恢复所有中间片段,最后将全新片段添加回顶部。
private void manageBackStack(Fragment frag){
FragmentManager fragManager = getCurrentActivity().getFragmentManager();
Fragment testFrag = fragManager.findFragmentByTag(frag.getClass().getSimpleName());
if(testFrag != null) { //If fragment already exists
int maxIndex = fragManager.getBackStackEntryCount() - 1;
Fragment[] stackPlaceholder = new Fragment[maxIndex];
//Store all fragments between new fragment and its old duplicate in an array
for (int i = maxIndex; i >= 0; i--) {
String newFragName = fragManager.getBackStackEntryAt(i).getName();
if(!newFragName.equals(frag.getClass().getSimpleName())) {
//Do not add fragments generated inside other fragments back on to the stack
if (!newFragName.equals("ITServiceCenterFragment")) {
stackPlaceholder[i - 1] = fragManager.findFragmentByTag(newFragName);
}
fragManager.popBackStackImmediate();
}else {
fragManager.beginTransaction().remove(fragManager.findFragmentByTag(newFragName)).commit();
fragManager.popBackStackImmediate();
break;
}
}
//Push the in-between fragments back onto the stack
for (int i = 0; i < maxIndex; i++) {
if (stackPlaceholder[i] != null) {
String fragName = stackPlaceholder[i].getClass().getSimpleName();
fragManager
.beginTransaction()
.replace(R.id.container, stackPlaceholder[i], fragName)
.addToBackStack(fragName)
.commit();
}
}
}
//Push most recent fragment onto stack
fragManager
.beginTransaction()
.replace(R.id.container, frag, frag.getClass().getSimpleName())
.addToBackStack(frag.getClass().getSimpleName())
.commit();
}