删除并重新添加片段后,保留getChildFragmentManager导航堆栈

我有活动:

`<RelativeLayout>

<FrameLayout

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

<Button/>

<Button/>

</RelativeLayout>`

在此容器内,取决于所按下的按钮是FragmentA还是FragmentB。这些片段是嵌套子片段的 。即,每个片段中都包含自己的导航堆栈。

在活动的onCreate中,我将实例化这两个片段:

fragmentA = (FragmentContainer) Fragment.instantiate(this,

FragmentContainer.class.getName()); fragmentB = (FragmentContainer)

Fragment.instantiate(this, FragmentContainer.class.getName());

然后,我继续互相替换:

final FragmentTransaction fragmentTransaction =

getSupportFragmentManager().beginTransaction() .replace(R.id.container,

fragment); .commitAllowingStateLoss();

到目前为止,一切都很好。但

每次我用fragmentB替换fragmentA时(反之亦然)

破坏其导航堆栈,fragmentA / B每次都从头开始,而不是替换之前包含的嵌套片段。

有任何想法吗?至少可行吗?康斯坦丁,祝你有美好的一天

回答:

所以这就是我解决的方法:

<RelativeLayout>

<FrameLayout

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</RelativeLayout>

public class MainActivity extends FragmentActivity {

final FragmentContainer [] fragmentContainers = new FragmentContainer[3];

int currentTabIndex = -1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final FragmentMetaData [] fragmentContainersMetaData = {

new FragmentMetaData(FragmentA.class.getName(), null),

new FragmentMetaData(FragmentB.class.getName(), null),

new FragmentMetaData(FragmentC.class.getName(), null)

};

for (int i = 0; i < fragmentContainers.length; i++) {

fragmentContainers[i] = (FragmentContainer) Fragment.instantiate(this, FragmentContainer.class.getName());

fragmentContainers[i].addMetaData(fragmentContainersMetaData[i]);

}

tabPageNavigationSelection(0);

}

void replaceFragmentBy(final Fragment fragment) {

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.container, fragment);

fragmentTransaction.commitAllowingStateLoss();

}

// Method to switch between tabs

void tabPageNavigationSelection (final int index) {

if (currentTabIndex == index) {

fragmentContainers[currentTabIndex].clearStackExceptRootFragment();

} else {

currentTabIndex = index;

replaceFragmentBy(fragmentContainers[currentTabIndex]);

}

}

}

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:background="@color/lightest_gray"

android:id="@+id/nestedContainer"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

public final class FragmentContainer extends Fragment {

private final List<FragmentMetaData> fragmentMetaDataStack = new ArrayList<>();

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

initialize(inflater, container, R.layout.fragment_container);

for (FragmentMetaData metaData : fragmentMetaDataStack) {

showNestedFragment(Fragment.instantiate(getActivity(), metaData.className, metaData.fragmentBundle), fragmentMetaDataStack.indexOf(metaData) > 0, true);

}

return getFragmentView();

}

@Override

public void onResume() {

super.onResume();

if (getChildFragmentManager().getFragments() == null){

return;

}

int stackDepth = getChildFragmentManager().getFragments().size();

if (stackDepth > 0 && fragmentMetaDataStack.size() < stackDepth &&

getChildFragmentManager().getFragments().get(fragmentMetaDataStack.size() - 1) != null) {

getChildFragmentManager().getFragments().get(fragmentMetaDataStack.size()-1).onResume();

}

}

public void showNestedFragment(final Fragment fragment, final boolean addToBackStack, final boolean isRestoring) {

final FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();

fragmentTransaction.replace(R.id.nestedContainer, fragment);

if (addToBackStack) {

fragmentTransaction.addToBackStack(null);

}

if (!isRestoring) {

fragmentMetaDataStack.add(new FragmentMetaData(fragment.getClass().getName(), fragment.getArguments()));

}

fragmentTransaction.commitAllowingStateLoss();

}

public void onBackPressed() {

if (getChildFragmentManager().getBackStackEntryCount() > 0) {

getChildFragmentManager().popBackStack();

fragmentMetaDataStack.remove(fragmentMetaDataStack.size() - 1);

} else {

getActivity().finish();

}

}

public void addMetaData(FragmentMetaData metaData) {

fragmentMetaDataStack.add(metaData);

}

public void clearStackExceptRootFragment() {

getChildFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

while (fragmentMetaDataStack.size() > 1) {

fragmentMetaDataStack.remove(1);

}

}

}

希望这会对尝试复制instagram导航模型的人有所帮助:)

以上是 删除并重新添加片段后,保留getChildFragmentManager导航堆栈 的全部内容, 来源链接: utcz.com/qa/410585.html

回到顶部